blob: f5b697bf39f26f51b3d0a2d767eb9d90fd9a8ca6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020020#include "xfs_acl.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_quota.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_rtalloc.h"
35#include "xfs_error.h"
36#include "xfs_itable.h"
37#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_buf_item.h"
40#include "xfs_utils.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100041#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080044#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/xattr.h>
46#include <linux/namei.h>
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020047#include <linux/posix_acl.h>
Nathan Scott446ada42006-01-11 15:35:44 +110048#include <linux/security.h>
Eric Sandeenf35642e2008-11-28 14:23:35 +110049#include <linux/fiemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Nathan Scott4aeb6642005-11-02 11:43:58 +110052/*
Christoph Hellwigf9581b12009-10-06 20:29:26 +000053 * Bring the timestamps in the XFS inode uptodate.
54 *
55 * Used before writing the inode to disk.
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110056 */
57void
Christoph Hellwigf9581b12009-10-06 20:29:26 +000058xfs_synchronize_times(
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110059 xfs_inode_t *ip)
60{
David Chinner01651642008-08-13 15:45:15 +100061 struct inode *inode = VFS_I(ip);
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110062
Christoph Hellwigf9581b12009-10-06 20:29:26 +000063 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
64 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
65 ip->i_d.di_ctime.t_sec = (__int32_t)inode->i_ctime.tv_sec;
66 ip->i_d.di_ctime.t_nsec = (__int32_t)inode->i_ctime.tv_nsec;
67 ip->i_d.di_mtime.t_sec = (__int32_t)inode->i_mtime.tv_sec;
68 ip->i_d.di_mtime.t_nsec = (__int32_t)inode->i_mtime.tv_nsec;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110069}
70
71/*
Christoph Hellwigafd717d2011-11-19 13:13:38 -050072 * If the linux inode is valid, mark it dirty, else mark the dirty state
73 * in the XFS inode to make sure we pick it up when reclaiming the inode.
David Chinner5d51eff2007-11-23 16:29:18 +110074 */
75void
76xfs_mark_inode_dirty_sync(
77 xfs_inode_t *ip)
78{
David Chinner01651642008-08-13 15:45:15 +100079 struct inode *inode = VFS_I(ip);
David Chinner5d51eff2007-11-23 16:29:18 +110080
Al Viroa4ffdde2010-06-02 17:38:30 -040081 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
Christoph Hellwigaf048192008-03-06 13:46:43 +110082 mark_inode_dirty_sync(inode);
Christoph Hellwigafd717d2011-11-19 13:13:38 -050083 else {
84 barrier();
85 ip->i_update_core = 1;
86 }
David Chinner5d51eff2007-11-23 16:29:18 +110087}
88
Christoph Hellwig66d834e2010-02-15 09:44:49 +000089void
90xfs_mark_inode_dirty(
91 xfs_inode_t *ip)
92{
93 struct inode *inode = VFS_I(ip);
94
Al Viroa4ffdde2010-06-02 17:38:30 -040095 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
Christoph Hellwig66d834e2010-02-15 09:44:49 +000096 mark_inode_dirty(inode);
Christoph Hellwigafd717d2011-11-19 13:13:38 -050097 else {
98 barrier();
99 ip->i_update_core = 1;
100 }
101
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000102}
103
David Chinner5d51eff2007-11-23 16:29:18 +1100104/*
Nathan Scott446ada42006-01-11 15:35:44 +1100105 * Hook in SELinux. This is not quite correct yet, what we really need
106 * here (as we do for default ACLs) is a mechanism by which creation of
107 * these attrs can be journalled at inode creation time (along with the
108 * inode, of course, such that log replay can't cause these to be lost).
109 */
110STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100111xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +1100112 struct inode *inode,
Eric Paris2a7dba32011-02-01 11:05:39 -0500113 struct inode *dir,
114 const struct qstr *qstr)
Nathan Scott446ada42006-01-11 15:35:44 +1100115{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100116 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100117 size_t length;
118 void *value;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100119 unsigned char *name;
Nathan Scott446ada42006-01-11 15:35:44 +1100120 int error;
121
Eric Paris2a7dba32011-02-01 11:05:39 -0500122 error = security_inode_init_security(inode, dir, qstr, (char **)&name,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100123 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100124 if (error) {
125 if (error == -EOPNOTSUPP)
126 return 0;
127 return -error;
128 }
129
Christoph Hellwigaf048192008-03-06 13:46:43 +1100130 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100131
132 kfree(name);
133 kfree(value);
134 return error;
135}
136
Barry Naujok556b8b12008-04-10 12:22:07 +1000137static void
138xfs_dentry_to_name(
139 struct xfs_name *namep,
140 struct dentry *dentry)
141{
142 namep->name = dentry->d_name.name;
143 namep->len = dentry->d_name.len;
144}
145
David Chinner7989cb82007-02-10 18:34:56 +1100146STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100147xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000148 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100149 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000150 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100151{
Barry Naujok556b8b12008-04-10 12:22:07 +1000152 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100153
154 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100155 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100156 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100157 * ENOSPC can hit here, among other things.
158 */
Barry Naujok556b8b12008-04-10 12:22:07 +1000159 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100160
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000161 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100162 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100166xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct inode *dir,
168 struct dentry *dentry,
169 int mode,
170 dev_t rdev)
171{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100172 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100173 struct xfs_inode *ip = NULL;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200174 struct posix_acl *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000175 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int error;
177
178 /*
179 * Irix uses Missed'em'V split, but doesn't want to see
180 * the upper 5 bits of (14bit) major.
181 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100182 if (S_ISCHR(mode) || S_ISBLK(mode)) {
183 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
184 return -EINVAL;
185 rdev = sysv_encode_dev(rdev);
186 } else {
187 rdev = 0;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200190 if (IS_POSIXACL(dir)) {
191 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
192 if (IS_ERR(default_acl))
Al Viroc46a1312011-06-05 11:12:31 +0000193 return PTR_ERR(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Christoph Hellwige83f1eb2009-06-12 11:19:11 -0400195 if (!default_acl)
196 mode &= ~current_umask();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100199 xfs_dentry_to_name(&name, dentry);
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000200 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100201 if (unlikely(error))
202 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100203
David Chinner01651642008-08-13 15:45:15 +1000204 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100205
Eric Paris2a7dba32011-02-01 11:05:39 -0500206 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100207 if (unlikely(error))
208 goto out_cleanup_inode;
209
210 if (default_acl) {
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200211 error = -xfs_inherit_acl(inode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100212 if (unlikely(error))
213 goto out_cleanup_inode;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200214 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100218 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100219 return -error;
220
221 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000222 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100223 out_free_acl:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200224 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -error;
226}
227
228STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100229xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct inode *dir,
231 struct dentry *dentry,
232 int mode,
233 struct nameidata *nd)
234{
Nathan Scott416c6d52006-03-14 14:00:51 +1100235 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100239xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct inode *dir,
241 struct dentry *dentry,
242 int mode)
243{
Nathan Scott416c6d52006-03-14 14:00:51 +1100244 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100248xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct inode *dir,
250 struct dentry *dentry,
251 struct nameidata *nd)
252{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100253 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000254 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int error;
256
257 if (dentry->d_name.len >= MAXNAMELEN)
258 return ERR_PTR(-ENAMETOOLONG);
259
Barry Naujok556b8b12008-04-10 12:22:07 +1000260 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000261 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000262 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (unlikely(error != ENOENT))
264 return ERR_PTR(-error);
265 d_add(dentry, NULL);
266 return NULL;
267 }
268
David Chinner01651642008-08-13 15:45:15 +1000269 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Barry Naujok384f3ce2008-05-21 16:58:22 +1000272STATIC struct dentry *
273xfs_vn_ci_lookup(
274 struct inode *dir,
275 struct dentry *dentry,
276 struct nameidata *nd)
277{
278 struct xfs_inode *ip;
279 struct xfs_name xname;
280 struct xfs_name ci_name;
281 struct qstr dname;
282 int error;
283
284 if (dentry->d_name.len >= MAXNAMELEN)
285 return ERR_PTR(-ENAMETOOLONG);
286
287 xfs_dentry_to_name(&xname, dentry);
288 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
289 if (unlikely(error)) {
290 if (unlikely(error != ENOENT))
291 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000292 /*
293 * call d_add(dentry, NULL) here when d_drop_negative_children
294 * is called in xfs_vn_mknod (ie. allow negative dentries
295 * with CI filesystems).
296 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000297 return NULL;
298 }
299
300 /* if exact match, just splice and exit */
301 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000302 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000303
304 /* else case-insensitive match... */
305 dname.name = ci_name.name;
306 dname.len = ci_name.len;
Christoph Hellwige45b5902008-08-07 23:49:07 +0200307 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000308 kmem_free(ci_name.name);
309 return dentry;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100313xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct dentry *old_dentry,
315 struct inode *dir,
316 struct dentry *dentry)
317{
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100318 struct inode *inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000319 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int error;
321
Barry Naujok556b8b12008-04-10 12:22:07 +1000322 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Barry Naujok556b8b12008-04-10 12:22:07 +1000324 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100325 if (unlikely(error))
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100326 return -error;
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100327
Al Viro7de9c6e2010-10-23 11:11:40 -0400328 ihold(inode);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100329 d_instantiate(dentry, inode);
330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100334xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct inode *dir,
336 struct dentry *dentry)
337{
Barry Naujok556b8b12008-04-10 12:22:07 +1000338 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int error;
340
Barry Naujok556b8b12008-04-10 12:22:07 +1000341 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Christoph Hellwige5700702008-06-23 13:25:25 +1000343 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
344 if (error)
345 return error;
346
347 /*
348 * With unlink, the VFS makes the dentry "negative": no inode,
349 * but still hashed. This is incompatible with case-insensitive
350 * mode, so invalidate (unhash) the dentry in CI-mode.
351 */
352 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
353 d_invalidate(dentry);
354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100358xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct inode *dir,
360 struct dentry *dentry,
361 const char *symname)
362{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100363 struct inode *inode;
364 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000365 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000367 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000369 mode = S_IFLNK |
Al Viroce3b0f82009-03-29 19:08:22 -0400370 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
Barry Naujok556b8b12008-04-10 12:22:07 +1000371 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000373 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100374 if (unlikely(error))
375 goto out;
376
David Chinner01651642008-08-13 15:45:15 +1000377 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100378
Eric Paris2a7dba32011-02-01 11:05:39 -0500379 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100380 if (unlikely(error))
381 goto out_cleanup_inode;
382
383 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100384 return 0;
385
386 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000387 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100388 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -error;
390}
391
392STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100393xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct inode *odir,
395 struct dentry *odentry,
396 struct inode *ndir,
397 struct dentry *ndentry)
398{
399 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000400 struct xfs_name oname;
401 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Barry Naujok556b8b12008-04-10 12:22:07 +1000403 xfs_dentry_to_name(&oname, odentry);
404 xfs_dentry_to_name(&nname, ndentry);
405
Christoph Hellwige5700702008-06-23 13:25:25 +1000406 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000407 XFS_I(ndir), &nname, new_inode ?
408 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/*
412 * careful here - this function can get called recursively, so
413 * we need to be very careful about how much stack we use.
414 * uio is kmalloced for this reason...
415 */
Al Viro008b1502005-08-20 00:17:39 +0100416STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100417xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct dentry *dentry,
419 struct nameidata *nd)
420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000422 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700424 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000425 if (!link)
426 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000428 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000429 if (unlikely(error))
430 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100433 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000434
435 out_kfree:
436 kfree(link);
437 out_err:
438 nd_set_link(nd, ERR_PTR(error));
439 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Nathan Scottcde410a2005-09-05 11:47:01 +1000442STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100443xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000444 struct dentry *dentry,
445 struct nameidata *nd,
446 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Nathan Scottcde410a2005-09-05 11:47:01 +1000448 char *s = nd_get_link(nd);
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (!IS_ERR(s))
451 kfree(s);
452}
453
Christoph Hellwig45767582008-02-05 12:13:24 +1100454STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100455xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000456 struct vfsmount *mnt,
457 struct dentry *dentry,
458 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000460 struct inode *inode = dentry->d_inode;
461 struct xfs_inode *ip = XFS_I(inode);
462 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000464 trace_xfs_getattr(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000465
466 if (XFS_FORCED_SHUTDOWN(mp))
Mitsuo Hayasaka5b9d69b2011-11-19 13:13:42 -0500467 return -XFS_ERROR(EIO);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000468
469 stat->size = XFS_ISIZE(ip);
470 stat->dev = inode->i_sb->s_dev;
471 stat->mode = ip->i_d.di_mode;
472 stat->nlink = ip->i_d.di_nlink;
473 stat->uid = ip->i_d.di_uid;
474 stat->gid = ip->i_d.di_gid;
475 stat->ino = ip->i_ino;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000476 stat->atime = inode->i_atime;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000477 stat->mtime = inode->i_mtime;
478 stat->ctime = inode->i_ctime;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000479 stat->blocks =
480 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
481
482
483 switch (inode->i_mode & S_IFMT) {
484 case S_IFBLK:
485 case S_IFCHR:
486 stat->blksize = BLKDEV_IOSIZE;
487 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
488 sysv_minor(ip->i_df.if_u2.if_rdev));
489 break;
490 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100491 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000492 /*
493 * If the file blocks are being allocated from a
494 * realtime volume, then return the inode's realtime
495 * extent size or the realtime volume's extent size.
496 */
497 stat->blksize =
498 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
499 } else
500 stat->blksize = xfs_preferred_iosize(mp);
501 stat->rdev = 0;
502 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000503 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000504
505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100509xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct dentry *dentry,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000511 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +1100513 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Eric Sandeenf35642e2008-11-28 14:23:35 +1100516#define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
517
518/*
519 * Call fiemap helper to fill in user data.
520 * Returns positive errors to xfs_getbmap.
521 */
522STATIC int
523xfs_fiemap_format(
524 void **arg,
525 struct getbmapx *bmv,
526 int *full)
527{
528 int error;
529 struct fiemap_extent_info *fieinfo = *arg;
530 u32 fiemap_flags = 0;
531 u64 logical, physical, length;
532
533 /* Do nothing for a hole */
534 if (bmv->bmv_block == -1LL)
535 return 0;
536
537 logical = BBTOB(bmv->bmv_offset);
538 physical = BBTOB(bmv->bmv_block);
539 length = BBTOB(bmv->bmv_length);
540
541 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
542 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
543 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
544 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
545 physical = 0; /* no block yet */
546 }
547 if (bmv->bmv_oflags & BMV_OF_LAST)
548 fiemap_flags |= FIEMAP_EXTENT_LAST;
549
550 error = fiemap_fill_next_extent(fieinfo, logical, physical,
551 length, fiemap_flags);
552 if (error > 0) {
553 error = 0;
554 *full = 1; /* user array now full */
555 }
556
557 return -error;
558}
559
560STATIC int
561xfs_vn_fiemap(
562 struct inode *inode,
563 struct fiemap_extent_info *fieinfo,
564 u64 start,
565 u64 length)
566{
567 xfs_inode_t *ip = XFS_I(inode);
568 struct getbmapx bm;
569 int error;
570
571 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
572 if (error)
573 return error;
574
575 /* Set up bmap header for xfs internal routine */
576 bm.bmv_offset = BTOBB(start);
577 /* Special case for whole file */
578 if (length == FIEMAP_MAX_OFFSET)
579 bm.bmv_length = -1LL;
580 else
581 bm.bmv_length = BTOBB(length);
582
Eric Sandeen97db39a2009-07-26 21:52:01 -0500583 /* We add one because in getbmap world count includes the header */
Tao Ma2d1ff3c2010-04-29 15:13:56 +1000584 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
585 fieinfo->fi_extents_max + 1;
586 bm.bmv_count = min_t(__s32, bm.bmv_count,
587 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
Tao Ma9af25462010-08-30 02:44:03 +0000588 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
Eric Sandeenf35642e2008-11-28 14:23:35 +1100589 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
590 bm.bmv_iflags |= BMV_IF_ATTRFORK;
591 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
592 bm.bmv_iflags |= BMV_IF_DELALLOC;
593
594 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
595 if (error)
596 return -error;
597
598 return 0;
599}
600
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000601static const struct inode_operations xfs_inode_operations = {
Linus Torvalds18f4c642009-08-28 12:29:03 -0700602 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100603 .getattr = xfs_vn_getattr,
604 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000605 .setxattr = generic_setxattr,
606 .getxattr = generic_getxattr,
607 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100608 .listxattr = xfs_vn_listxattr,
Eric Sandeenf35642e2008-11-28 14:23:35 +1100609 .fiemap = xfs_vn_fiemap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610};
611
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000612static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100613 .create = xfs_vn_create,
614 .lookup = xfs_vn_lookup,
615 .link = xfs_vn_link,
616 .unlink = xfs_vn_unlink,
617 .symlink = xfs_vn_symlink,
618 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000619 /*
620 * Yes, XFS uses the same method for rmdir and unlink.
621 *
622 * There are some subtile differences deeper in the code,
623 * but we use S_ISDIR to check for those.
624 */
625 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100626 .mknod = xfs_vn_mknod,
627 .rename = xfs_vn_rename,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700628 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100629 .getattr = xfs_vn_getattr,
630 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000631 .setxattr = generic_setxattr,
632 .getxattr = generic_getxattr,
633 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100634 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000637static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +1000638 .create = xfs_vn_create,
639 .lookup = xfs_vn_ci_lookup,
640 .link = xfs_vn_link,
641 .unlink = xfs_vn_unlink,
642 .symlink = xfs_vn_symlink,
643 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000644 /*
645 * Yes, XFS uses the same method for rmdir and unlink.
646 *
647 * There are some subtile differences deeper in the code,
648 * but we use S_ISDIR to check for those.
649 */
650 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000651 .mknod = xfs_vn_mknod,
652 .rename = xfs_vn_rename,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700653 .check_acl = xfs_check_acl,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000654 .getattr = xfs_vn_getattr,
655 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000656 .setxattr = generic_setxattr,
657 .getxattr = generic_getxattr,
658 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000659 .listxattr = xfs_vn_listxattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000660};
661
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000662static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100664 .follow_link = xfs_vn_follow_link,
665 .put_link = xfs_vn_put_link,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700666 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100667 .getattr = xfs_vn_getattr,
668 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000669 .setxattr = generic_setxattr,
670 .getxattr = generic_getxattr,
671 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100672 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673};
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000674
675STATIC void
676xfs_diflags_to_iflags(
677 struct inode *inode,
678 struct xfs_inode *ip)
679{
680 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
681 inode->i_flags |= S_IMMUTABLE;
682 else
683 inode->i_flags &= ~S_IMMUTABLE;
684 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
685 inode->i_flags |= S_APPEND;
686 else
687 inode->i_flags &= ~S_APPEND;
688 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
689 inode->i_flags |= S_SYNC;
690 else
691 inode->i_flags &= ~S_SYNC;
692 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
693 inode->i_flags |= S_NOATIME;
694 else
695 inode->i_flags &= ~S_NOATIME;
696}
697
698/*
699 * Initialize the Linux inode, set up the operation vectors and
700 * unlock the inode.
701 *
702 * When reading existing inodes from disk this is called directly
703 * from xfs_iget, when creating a new inode it is called from
704 * xfs_ialloc after setting up the inode.
David Chinnerbf904242008-10-30 17:36:14 +1100705 *
706 * We are always called with an uninitialised linux inode here.
707 * We need to initialise the necessary fields and take a reference
708 * on it.
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000709 */
710void
711xfs_setup_inode(
712 struct xfs_inode *ip)
713{
David Chinnerbf904242008-10-30 17:36:14 +1100714 struct inode *inode = &ip->i_vnode;
715
716 inode->i_ino = ip->i_ino;
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100717 inode->i_state = I_NEW;
Christoph Hellwig646ec462010-10-23 07:15:32 -0400718
719 inode_sb_list_add(inode);
Christoph Hellwigc6f6cd02010-11-06 11:43:08 +0000720 /* make the inode look hashed for the writeback code */
721 hlist_add_fake(&inode->i_hash);
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000722
723 inode->i_mode = ip->i_d.di_mode;
724 inode->i_nlink = ip->i_d.di_nlink;
725 inode->i_uid = ip->i_d.di_uid;
726 inode->i_gid = ip->i_d.di_gid;
727
728 switch (inode->i_mode & S_IFMT) {
729 case S_IFBLK:
730 case S_IFCHR:
731 inode->i_rdev =
732 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
733 sysv_minor(ip->i_df.if_u2.if_rdev));
734 break;
735 default:
736 inode->i_rdev = 0;
737 break;
738 }
739
740 inode->i_generation = ip->i_d.di_gen;
741 i_size_write(inode, ip->i_d.di_size);
742 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
743 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
744 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
745 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
746 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
747 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
748 xfs_diflags_to_iflags(inode, ip);
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000749
750 switch (inode->i_mode & S_IFMT) {
751 case S_IFREG:
752 inode->i_op = &xfs_inode_operations;
753 inode->i_fop = &xfs_file_operations;
754 inode->i_mapping->a_ops = &xfs_address_space_operations;
755 break;
756 case S_IFDIR:
757 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
758 inode->i_op = &xfs_dir_ci_inode_operations;
759 else
760 inode->i_op = &xfs_dir_inode_operations;
761 inode->i_fop = &xfs_dir_file_operations;
762 break;
763 case S_IFLNK:
764 inode->i_op = &xfs_symlink_inode_operations;
765 if (!(ip->i_df.if_flags & XFS_IFINLINE))
766 inode->i_mapping->a_ops = &xfs_address_space_operations;
767 break;
768 default:
769 inode->i_op = &xfs_inode_operations;
770 init_special_inode(inode, inode->i_mode, inode->i_rdev);
771 break;
772 }
773
774 xfs_iflags_clear(ip, XFS_INEW);
775 barrier();
776
777 unlock_new_inode(inode);
778}