blob: 9ff7fc603d2f4325c550303d2f6295c731b65e82 [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/*
David Chinnerbf904242008-10-30 17:36:14 +110072 * If the linux inode is valid, mark it dirty.
David Chinner5d51eff2007-11-23 16:29:18 +110073 * Used when commiting a dirty inode into a transaction so that
74 * the inode will get written back by the linux code
75 */
76void
77xfs_mark_inode_dirty_sync(
78 xfs_inode_t *ip)
79{
David Chinner01651642008-08-13 15:45:15 +100080 struct inode *inode = VFS_I(ip);
David Chinner5d51eff2007-11-23 16:29:18 +110081
Al Viroa4ffdde2010-06-02 17:38:30 -040082 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
Christoph Hellwigaf048192008-03-06 13:46:43 +110083 mark_inode_dirty_sync(inode);
David Chinner5d51eff2007-11-23 16:29:18 +110084}
85
Christoph Hellwig66d834e2010-02-15 09:44:49 +000086void
87xfs_mark_inode_dirty(
88 xfs_inode_t *ip)
89{
90 struct inode *inode = VFS_I(ip);
91
Al Viroa4ffdde2010-06-02 17:38:30 -040092 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
Christoph Hellwig66d834e2010-02-15 09:44:49 +000093 mark_inode_dirty(inode);
94}
95
David Chinner5d51eff2007-11-23 16:29:18 +110096/*
Nathan Scott446ada42006-01-11 15:35:44 +110097 * Hook in SELinux. This is not quite correct yet, what we really need
98 * here (as we do for default ACLs) is a mechanism by which creation of
99 * these attrs can be journalled at inode creation time (along with the
100 * inode, of course, such that log replay can't cause these to be lost).
101 */
102STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100103xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +1100104 struct inode *inode,
Eric Paris2a7dba32011-02-01 11:05:39 -0500105 struct inode *dir,
106 const struct qstr *qstr)
Nathan Scott446ada42006-01-11 15:35:44 +1100107{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100108 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100109 size_t length;
110 void *value;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100111 unsigned char *name;
Nathan Scott446ada42006-01-11 15:35:44 +1100112 int error;
113
Eric Paris2a7dba32011-02-01 11:05:39 -0500114 error = security_inode_init_security(inode, dir, qstr, (char **)&name,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100115 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100116 if (error) {
117 if (error == -EOPNOTSUPP)
118 return 0;
119 return -error;
120 }
121
Christoph Hellwigaf048192008-03-06 13:46:43 +1100122 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100123
124 kfree(name);
125 kfree(value);
126 return error;
127}
128
Barry Naujok556b8b12008-04-10 12:22:07 +1000129static void
130xfs_dentry_to_name(
131 struct xfs_name *namep,
132 struct dentry *dentry)
133{
134 namep->name = dentry->d_name.name;
135 namep->len = dentry->d_name.len;
136}
137
David Chinner7989cb82007-02-10 18:34:56 +1100138STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100139xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000140 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100141 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000142 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100143{
Barry Naujok556b8b12008-04-10 12:22:07 +1000144 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100145
146 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100147 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100148 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100149 * ENOSPC can hit here, among other things.
150 */
Barry Naujok556b8b12008-04-10 12:22:07 +1000151 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100152
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000153 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100154 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100158xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct inode *dir,
160 struct dentry *dentry,
161 int mode,
162 dev_t rdev)
163{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100164 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100165 struct xfs_inode *ip = NULL;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200166 struct posix_acl *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000167 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int error;
169
170 /*
171 * Irix uses Missed'em'V split, but doesn't want to see
172 * the upper 5 bits of (14bit) major.
173 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100174 if (S_ISCHR(mode) || S_ISBLK(mode)) {
175 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
176 return -EINVAL;
177 rdev = sysv_encode_dev(rdev);
178 } else {
179 rdev = 0;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200182 if (IS_POSIXACL(dir)) {
183 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
184 if (IS_ERR(default_acl))
185 return -PTR_ERR(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Christoph Hellwige83f1eb2009-06-12 11:19:11 -0400187 if (!default_acl)
188 mode &= ~current_umask();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100191 xfs_dentry_to_name(&name, dentry);
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000192 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100193 if (unlikely(error))
194 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100195
David Chinner01651642008-08-13 15:45:15 +1000196 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100197
Eric Paris2a7dba32011-02-01 11:05:39 -0500198 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100199 if (unlikely(error))
200 goto out_cleanup_inode;
201
202 if (default_acl) {
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200203 error = -xfs_inherit_acl(inode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100204 if (unlikely(error))
205 goto out_cleanup_inode;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200206 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100210 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100211 return -error;
212
213 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000214 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100215 out_free_acl:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200216 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return -error;
218}
219
220STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100221xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct inode *dir,
223 struct dentry *dentry,
224 int mode,
225 struct nameidata *nd)
226{
Nathan Scott416c6d52006-03-14 14:00:51 +1100227 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100231xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct inode *dir,
233 struct dentry *dentry,
234 int mode)
235{
Nathan Scott416c6d52006-03-14 14:00:51 +1100236 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100240xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct inode *dir,
242 struct dentry *dentry,
243 struct nameidata *nd)
244{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100245 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000246 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int error;
248
249 if (dentry->d_name.len >= MAXNAMELEN)
250 return ERR_PTR(-ENAMETOOLONG);
251
Barry Naujok556b8b12008-04-10 12:22:07 +1000252 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000253 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000254 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (unlikely(error != ENOENT))
256 return ERR_PTR(-error);
257 d_add(dentry, NULL);
258 return NULL;
259 }
260
David Chinner01651642008-08-13 15:45:15 +1000261 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Barry Naujok384f3ce2008-05-21 16:58:22 +1000264STATIC struct dentry *
265xfs_vn_ci_lookup(
266 struct inode *dir,
267 struct dentry *dentry,
268 struct nameidata *nd)
269{
270 struct xfs_inode *ip;
271 struct xfs_name xname;
272 struct xfs_name ci_name;
273 struct qstr dname;
274 int error;
275
276 if (dentry->d_name.len >= MAXNAMELEN)
277 return ERR_PTR(-ENAMETOOLONG);
278
279 xfs_dentry_to_name(&xname, dentry);
280 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
281 if (unlikely(error)) {
282 if (unlikely(error != ENOENT))
283 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000284 /*
285 * call d_add(dentry, NULL) here when d_drop_negative_children
286 * is called in xfs_vn_mknod (ie. allow negative dentries
287 * with CI filesystems).
288 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000289 return NULL;
290 }
291
292 /* if exact match, just splice and exit */
293 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000294 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000295
296 /* else case-insensitive match... */
297 dname.name = ci_name.name;
298 dname.len = ci_name.len;
Christoph Hellwige45b5902008-08-07 23:49:07 +0200299 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000300 kmem_free(ci_name.name);
301 return dentry;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100305xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct dentry *old_dentry,
307 struct inode *dir,
308 struct dentry *dentry)
309{
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100310 struct inode *inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000311 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int error;
313
Barry Naujok556b8b12008-04-10 12:22:07 +1000314 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Barry Naujok556b8b12008-04-10 12:22:07 +1000316 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100317 if (unlikely(error))
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100318 return -error;
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100319
Al Viro7de9c6ee2010-10-23 11:11:40 -0400320 ihold(inode);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100321 d_instantiate(dentry, inode);
322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100326xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct inode *dir,
328 struct dentry *dentry)
329{
Barry Naujok556b8b12008-04-10 12:22:07 +1000330 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int error;
332
Barry Naujok556b8b12008-04-10 12:22:07 +1000333 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Christoph Hellwige5700702008-06-23 13:25:25 +1000335 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
336 if (error)
337 return error;
338
339 /*
340 * With unlink, the VFS makes the dentry "negative": no inode,
341 * but still hashed. This is incompatible with case-insensitive
342 * mode, so invalidate (unhash) the dentry in CI-mode.
343 */
344 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
345 d_invalidate(dentry);
346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100350xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct inode *dir,
352 struct dentry *dentry,
353 const char *symname)
354{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100355 struct inode *inode;
356 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000357 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000359 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000361 mode = S_IFLNK |
Al Viroce3b0f82009-03-29 19:08:22 -0400362 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
Barry Naujok556b8b12008-04-10 12:22:07 +1000363 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000365 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100366 if (unlikely(error))
367 goto out;
368
David Chinner01651642008-08-13 15:45:15 +1000369 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100370
Eric Paris2a7dba32011-02-01 11:05:39 -0500371 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100372 if (unlikely(error))
373 goto out_cleanup_inode;
374
375 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100376 return 0;
377
378 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000379 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100380 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -error;
382}
383
384STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100385xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct inode *odir,
387 struct dentry *odentry,
388 struct inode *ndir,
389 struct dentry *ndentry)
390{
391 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000392 struct xfs_name oname;
393 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Barry Naujok556b8b12008-04-10 12:22:07 +1000395 xfs_dentry_to_name(&oname, odentry);
396 xfs_dentry_to_name(&nname, ndentry);
397
Christoph Hellwige5700702008-06-23 13:25:25 +1000398 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000399 XFS_I(ndir), &nname, new_inode ?
400 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403/*
404 * careful here - this function can get called recursively, so
405 * we need to be very careful about how much stack we use.
406 * uio is kmalloced for this reason...
407 */
Al Viro008b1502005-08-20 00:17:39 +0100408STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100409xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct dentry *dentry,
411 struct nameidata *nd)
412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000414 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700416 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000417 if (!link)
418 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000420 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000421 if (unlikely(error))
422 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100425 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000426
427 out_kfree:
428 kfree(link);
429 out_err:
430 nd_set_link(nd, ERR_PTR(error));
431 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Nathan Scottcde410a2005-09-05 11:47:01 +1000434STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100435xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000436 struct dentry *dentry,
437 struct nameidata *nd,
438 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Nathan Scottcde410a2005-09-05 11:47:01 +1000440 char *s = nd_get_link(nd);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!IS_ERR(s))
443 kfree(s);
444}
445
Christoph Hellwig45767582008-02-05 12:13:24 +1100446STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100447xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000448 struct vfsmount *mnt,
449 struct dentry *dentry,
450 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000452 struct inode *inode = dentry->d_inode;
453 struct xfs_inode *ip = XFS_I(inode);
454 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000456 trace_xfs_getattr(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000457
458 if (XFS_FORCED_SHUTDOWN(mp))
459 return XFS_ERROR(EIO);
460
461 stat->size = XFS_ISIZE(ip);
462 stat->dev = inode->i_sb->s_dev;
463 stat->mode = ip->i_d.di_mode;
464 stat->nlink = ip->i_d.di_nlink;
465 stat->uid = ip->i_d.di_uid;
466 stat->gid = ip->i_d.di_gid;
467 stat->ino = ip->i_ino;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000468 stat->atime = inode->i_atime;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000469 stat->mtime = inode->i_mtime;
470 stat->ctime = inode->i_ctime;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000471 stat->blocks =
472 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
473
474
475 switch (inode->i_mode & S_IFMT) {
476 case S_IFBLK:
477 case S_IFCHR:
478 stat->blksize = BLKDEV_IOSIZE;
479 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
480 sysv_minor(ip->i_df.if_u2.if_rdev));
481 break;
482 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100483 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000484 /*
485 * If the file blocks are being allocated from a
486 * realtime volume, then return the inode's realtime
487 * extent size or the realtime volume's extent size.
488 */
489 stat->blksize =
490 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
491 } else
492 stat->blksize = xfs_preferred_iosize(mp);
493 stat->rdev = 0;
494 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000495 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000496
497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100501xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct dentry *dentry,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000503 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +1100505 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Eric Sandeenf35642e2008-11-28 14:23:35 +1100508#define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
509
510/*
511 * Call fiemap helper to fill in user data.
512 * Returns positive errors to xfs_getbmap.
513 */
514STATIC int
515xfs_fiemap_format(
516 void **arg,
517 struct getbmapx *bmv,
518 int *full)
519{
520 int error;
521 struct fiemap_extent_info *fieinfo = *arg;
522 u32 fiemap_flags = 0;
523 u64 logical, physical, length;
524
525 /* Do nothing for a hole */
526 if (bmv->bmv_block == -1LL)
527 return 0;
528
529 logical = BBTOB(bmv->bmv_offset);
530 physical = BBTOB(bmv->bmv_block);
531 length = BBTOB(bmv->bmv_length);
532
533 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
534 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
535 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
536 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
537 physical = 0; /* no block yet */
538 }
539 if (bmv->bmv_oflags & BMV_OF_LAST)
540 fiemap_flags |= FIEMAP_EXTENT_LAST;
541
542 error = fiemap_fill_next_extent(fieinfo, logical, physical,
543 length, fiemap_flags);
544 if (error > 0) {
545 error = 0;
546 *full = 1; /* user array now full */
547 }
548
549 return -error;
550}
551
552STATIC int
553xfs_vn_fiemap(
554 struct inode *inode,
555 struct fiemap_extent_info *fieinfo,
556 u64 start,
557 u64 length)
558{
559 xfs_inode_t *ip = XFS_I(inode);
560 struct getbmapx bm;
561 int error;
562
563 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
564 if (error)
565 return error;
566
567 /* Set up bmap header for xfs internal routine */
568 bm.bmv_offset = BTOBB(start);
569 /* Special case for whole file */
570 if (length == FIEMAP_MAX_OFFSET)
571 bm.bmv_length = -1LL;
572 else
573 bm.bmv_length = BTOBB(length);
574
Eric Sandeen97db39a2009-07-26 21:52:01 -0500575 /* We add one because in getbmap world count includes the header */
Tao Ma2d1ff3c2010-04-29 15:13:56 +1000576 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
577 fieinfo->fi_extents_max + 1;
578 bm.bmv_count = min_t(__s32, bm.bmv_count,
579 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
Tao Ma9af25462010-08-30 02:44:03 +0000580 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
Eric Sandeenf35642e2008-11-28 14:23:35 +1100581 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
582 bm.bmv_iflags |= BMV_IF_ATTRFORK;
583 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
584 bm.bmv_iflags |= BMV_IF_DELALLOC;
585
586 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
587 if (error)
588 return -error;
589
590 return 0;
591}
592
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000593static const struct inode_operations xfs_inode_operations = {
Linus Torvalds18f4c642009-08-28 12:29:03 -0700594 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100595 .getattr = xfs_vn_getattr,
596 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000597 .setxattr = generic_setxattr,
598 .getxattr = generic_getxattr,
599 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100600 .listxattr = xfs_vn_listxattr,
Eric Sandeenf35642e2008-11-28 14:23:35 +1100601 .fiemap = xfs_vn_fiemap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602};
603
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000604static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100605 .create = xfs_vn_create,
606 .lookup = xfs_vn_lookup,
607 .link = xfs_vn_link,
608 .unlink = xfs_vn_unlink,
609 .symlink = xfs_vn_symlink,
610 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000611 /*
612 * Yes, XFS uses the same method for rmdir and unlink.
613 *
614 * There are some subtile differences deeper in the code,
615 * but we use S_ISDIR to check for those.
616 */
617 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100618 .mknod = xfs_vn_mknod,
619 .rename = xfs_vn_rename,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700620 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100621 .getattr = xfs_vn_getattr,
622 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000623 .setxattr = generic_setxattr,
624 .getxattr = generic_getxattr,
625 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100626 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627};
628
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000629static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +1000630 .create = xfs_vn_create,
631 .lookup = xfs_vn_ci_lookup,
632 .link = xfs_vn_link,
633 .unlink = xfs_vn_unlink,
634 .symlink = xfs_vn_symlink,
635 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000636 /*
637 * Yes, XFS uses the same method for rmdir and unlink.
638 *
639 * There are some subtile differences deeper in the code,
640 * but we use S_ISDIR to check for those.
641 */
642 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000643 .mknod = xfs_vn_mknod,
644 .rename = xfs_vn_rename,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700645 .check_acl = xfs_check_acl,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000646 .getattr = xfs_vn_getattr,
647 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000648 .setxattr = generic_setxattr,
649 .getxattr = generic_getxattr,
650 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000651 .listxattr = xfs_vn_listxattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000652};
653
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000654static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100656 .follow_link = xfs_vn_follow_link,
657 .put_link = xfs_vn_put_link,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700658 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100659 .getattr = xfs_vn_getattr,
660 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000661 .setxattr = generic_setxattr,
662 .getxattr = generic_getxattr,
663 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100664 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665};
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000666
667STATIC void
668xfs_diflags_to_iflags(
669 struct inode *inode,
670 struct xfs_inode *ip)
671{
672 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
673 inode->i_flags |= S_IMMUTABLE;
674 else
675 inode->i_flags &= ~S_IMMUTABLE;
676 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
677 inode->i_flags |= S_APPEND;
678 else
679 inode->i_flags &= ~S_APPEND;
680 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
681 inode->i_flags |= S_SYNC;
682 else
683 inode->i_flags &= ~S_SYNC;
684 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
685 inode->i_flags |= S_NOATIME;
686 else
687 inode->i_flags &= ~S_NOATIME;
688}
689
690/*
691 * Initialize the Linux inode, set up the operation vectors and
692 * unlock the inode.
693 *
694 * When reading existing inodes from disk this is called directly
695 * from xfs_iget, when creating a new inode it is called from
696 * xfs_ialloc after setting up the inode.
David Chinnerbf904242008-10-30 17:36:14 +1100697 *
698 * We are always called with an uninitialised linux inode here.
699 * We need to initialise the necessary fields and take a reference
700 * on it.
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000701 */
702void
703xfs_setup_inode(
704 struct xfs_inode *ip)
705{
David Chinnerbf904242008-10-30 17:36:14 +1100706 struct inode *inode = &ip->i_vnode;
707
708 inode->i_ino = ip->i_ino;
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100709 inode->i_state = I_NEW;
Christoph Hellwig646ec462010-10-23 07:15:32 -0400710
711 inode_sb_list_add(inode);
Christoph Hellwigc6f6cd02010-11-06 11:43:08 +0000712 /* make the inode look hashed for the writeback code */
713 hlist_add_fake(&inode->i_hash);
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000714
715 inode->i_mode = ip->i_d.di_mode;
716 inode->i_nlink = ip->i_d.di_nlink;
717 inode->i_uid = ip->i_d.di_uid;
718 inode->i_gid = ip->i_d.di_gid;
719
720 switch (inode->i_mode & S_IFMT) {
721 case S_IFBLK:
722 case S_IFCHR:
723 inode->i_rdev =
724 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
725 sysv_minor(ip->i_df.if_u2.if_rdev));
726 break;
727 default:
728 inode->i_rdev = 0;
729 break;
730 }
731
732 inode->i_generation = ip->i_d.di_gen;
733 i_size_write(inode, ip->i_d.di_size);
734 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
735 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
736 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
737 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
738 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
739 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
740 xfs_diflags_to_iflags(inode, ip);
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000741
742 switch (inode->i_mode & S_IFMT) {
743 case S_IFREG:
744 inode->i_op = &xfs_inode_operations;
745 inode->i_fop = &xfs_file_operations;
746 inode->i_mapping->a_ops = &xfs_address_space_operations;
747 break;
748 case S_IFDIR:
749 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
750 inode->i_op = &xfs_dir_ci_inode_operations;
751 else
752 inode->i_op = &xfs_dir_inode_operations;
753 inode->i_fop = &xfs_dir_file_operations;
754 break;
755 case S_IFLNK:
756 inode->i_op = &xfs_symlink_inode_operations;
757 if (!(ip->i_df.if_flags & XFS_IFINLINE))
758 inode->i_mapping->a_ops = &xfs_address_space_operations;
759 break;
760 default:
761 inode->i_op = &xfs_inode_operations;
762 init_special_inode(inode, inode->i_mode, inode->i_rdev);
763 break;
764 }
765
766 xfs_iflags_clear(ip, XFS_INEW);
767 barrier();
768
769 unlock_new_inode(inode);
770}