blob: bd5727852fd6306e692984af2b2830a3dfdddb34 [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,
Nathan Scott446ada42006-01-11 15:35:44 +1100105 struct inode *dir)
106{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100107 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100108 size_t length;
109 void *value;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100110 unsigned char *name;
Nathan Scott446ada42006-01-11 15:35:44 +1100111 int error;
112
Dave Chinnera9273ca2010-01-20 10:47:48 +1100113 error = security_inode_init_security(inode, dir, (char **)&name,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100114 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100115 if (error) {
116 if (error == -EOPNOTSUPP)
117 return 0;
118 return -error;
119 }
120
Christoph Hellwigaf048192008-03-06 13:46:43 +1100121 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100122
123 kfree(name);
124 kfree(value);
125 return error;
126}
127
Barry Naujok556b8b12008-04-10 12:22:07 +1000128static void
129xfs_dentry_to_name(
130 struct xfs_name *namep,
131 struct dentry *dentry)
132{
133 namep->name = dentry->d_name.name;
134 namep->len = dentry->d_name.len;
135}
136
David Chinner7989cb82007-02-10 18:34:56 +1100137STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100138xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000139 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100140 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000141 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100142{
Barry Naujok556b8b12008-04-10 12:22:07 +1000143 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100144
145 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100146 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100147 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100148 * ENOSPC can hit here, among other things.
149 */
Barry Naujok556b8b12008-04-10 12:22:07 +1000150 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100151
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000152 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100153 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100157xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct inode *dir,
159 struct dentry *dentry,
160 int mode,
161 dev_t rdev)
162{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100163 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100164 struct xfs_inode *ip = NULL;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200165 struct posix_acl *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000166 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int error;
168
169 /*
170 * Irix uses Missed'em'V split, but doesn't want to see
171 * the upper 5 bits of (14bit) major.
172 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100173 if (S_ISCHR(mode) || S_ISBLK(mode)) {
174 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
175 return -EINVAL;
176 rdev = sysv_encode_dev(rdev);
177 } else {
178 rdev = 0;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200181 if (IS_POSIXACL(dir)) {
182 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
183 if (IS_ERR(default_acl))
184 return -PTR_ERR(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Christoph Hellwige83f1eb2009-06-12 11:19:11 -0400186 if (!default_acl)
187 mode &= ~current_umask();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100190 xfs_dentry_to_name(&name, dentry);
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000191 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100192 if (unlikely(error))
193 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100194
David Chinner01651642008-08-13 15:45:15 +1000195 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100196
197 error = xfs_init_security(inode, dir);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100198 if (unlikely(error))
199 goto out_cleanup_inode;
200
201 if (default_acl) {
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200202 error = -xfs_inherit_acl(inode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100203 if (unlikely(error))
204 goto out_cleanup_inode;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200205 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100209 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100210 return -error;
211
212 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000213 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100214 out_free_acl:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200215 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -error;
217}
218
219STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100220xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct inode *dir,
222 struct dentry *dentry,
223 int mode,
224 struct nameidata *nd)
225{
Nathan Scott416c6d52006-03-14 14:00:51 +1100226 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100230xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct inode *dir,
232 struct dentry *dentry,
233 int mode)
234{
Nathan Scott416c6d52006-03-14 14:00:51 +1100235 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100239xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct inode *dir,
241 struct dentry *dentry,
242 struct nameidata *nd)
243{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100244 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000245 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int error;
247
248 if (dentry->d_name.len >= MAXNAMELEN)
249 return ERR_PTR(-ENAMETOOLONG);
250
Barry Naujok556b8b12008-04-10 12:22:07 +1000251 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000252 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000253 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (unlikely(error != ENOENT))
255 return ERR_PTR(-error);
256 d_add(dentry, NULL);
257 return NULL;
258 }
259
David Chinner01651642008-08-13 15:45:15 +1000260 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Barry Naujok384f3ce2008-05-21 16:58:22 +1000263STATIC struct dentry *
264xfs_vn_ci_lookup(
265 struct inode *dir,
266 struct dentry *dentry,
267 struct nameidata *nd)
268{
269 struct xfs_inode *ip;
270 struct xfs_name xname;
271 struct xfs_name ci_name;
272 struct qstr dname;
273 int error;
274
275 if (dentry->d_name.len >= MAXNAMELEN)
276 return ERR_PTR(-ENAMETOOLONG);
277
278 xfs_dentry_to_name(&xname, dentry);
279 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
280 if (unlikely(error)) {
281 if (unlikely(error != ENOENT))
282 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000283 /*
284 * call d_add(dentry, NULL) here when d_drop_negative_children
285 * is called in xfs_vn_mknod (ie. allow negative dentries
286 * with CI filesystems).
287 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000288 return NULL;
289 }
290
291 /* if exact match, just splice and exit */
292 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000293 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000294
295 /* else case-insensitive match... */
296 dname.name = ci_name.name;
297 dname.len = ci_name.len;
Christoph Hellwige45b5902008-08-07 23:49:07 +0200298 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000299 kmem_free(ci_name.name);
300 return dentry;
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100304xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct dentry *old_dentry,
306 struct inode *dir,
307 struct dentry *dentry)
308{
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100309 struct inode *inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000310 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int error;
312
Barry Naujok556b8b12008-04-10 12:22:07 +1000313 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Barry Naujok556b8b12008-04-10 12:22:07 +1000315 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100316 if (unlikely(error))
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100317 return -error;
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100318
Al Viro7de9c6ee2010-10-23 11:11:40 -0400319 ihold(inode);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100320 d_instantiate(dentry, inode);
321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100325xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct inode *dir,
327 struct dentry *dentry)
328{
Barry Naujok556b8b12008-04-10 12:22:07 +1000329 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int error;
331
Barry Naujok556b8b12008-04-10 12:22:07 +1000332 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Christoph Hellwige5700702008-06-23 13:25:25 +1000334 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
335 if (error)
336 return error;
337
338 /*
339 * With unlink, the VFS makes the dentry "negative": no inode,
340 * but still hashed. This is incompatible with case-insensitive
341 * mode, so invalidate (unhash) the dentry in CI-mode.
342 */
343 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
344 d_invalidate(dentry);
345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100349xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct inode *dir,
351 struct dentry *dentry,
352 const char *symname)
353{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100354 struct inode *inode;
355 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000356 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000358 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000360 mode = S_IFLNK |
Al Viroce3b0f82009-03-29 19:08:22 -0400361 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
Barry Naujok556b8b12008-04-10 12:22:07 +1000362 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000364 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100365 if (unlikely(error))
366 goto out;
367
David Chinner01651642008-08-13 15:45:15 +1000368 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100369
370 error = xfs_init_security(inode, dir);
371 if (unlikely(error))
372 goto out_cleanup_inode;
373
374 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100375 return 0;
376
377 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000378 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100379 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -error;
381}
382
383STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100384xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct inode *odir,
386 struct dentry *odentry,
387 struct inode *ndir,
388 struct dentry *ndentry)
389{
390 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000391 struct xfs_name oname;
392 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Barry Naujok556b8b12008-04-10 12:22:07 +1000394 xfs_dentry_to_name(&oname, odentry);
395 xfs_dentry_to_name(&nname, ndentry);
396
Christoph Hellwige5700702008-06-23 13:25:25 +1000397 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000398 XFS_I(ndir), &nname, new_inode ?
399 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402/*
403 * careful here - this function can get called recursively, so
404 * we need to be very careful about how much stack we use.
405 * uio is kmalloced for this reason...
406 */
Al Viro008b1502005-08-20 00:17:39 +0100407STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100408xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct dentry *dentry,
410 struct nameidata *nd)
411{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000413 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700415 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000416 if (!link)
417 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000419 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000420 if (unlikely(error))
421 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100424 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000425
426 out_kfree:
427 kfree(link);
428 out_err:
429 nd_set_link(nd, ERR_PTR(error));
430 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Nathan Scottcde410a2005-09-05 11:47:01 +1000433STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100434xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000435 struct dentry *dentry,
436 struct nameidata *nd,
437 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Nathan Scottcde410a2005-09-05 11:47:01 +1000439 char *s = nd_get_link(nd);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!IS_ERR(s))
442 kfree(s);
443}
444
Christoph Hellwig45767582008-02-05 12:13:24 +1100445STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100446xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000447 struct vfsmount *mnt,
448 struct dentry *dentry,
449 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000451 struct inode *inode = dentry->d_inode;
452 struct xfs_inode *ip = XFS_I(inode);
453 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000455 trace_xfs_getattr(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000456
457 if (XFS_FORCED_SHUTDOWN(mp))
458 return XFS_ERROR(EIO);
459
460 stat->size = XFS_ISIZE(ip);
461 stat->dev = inode->i_sb->s_dev;
462 stat->mode = ip->i_d.di_mode;
463 stat->nlink = ip->i_d.di_nlink;
464 stat->uid = ip->i_d.di_uid;
465 stat->gid = ip->i_d.di_gid;
466 stat->ino = ip->i_ino;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000467 stat->atime = inode->i_atime;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000468 stat->mtime = inode->i_mtime;
469 stat->ctime = inode->i_ctime;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000470 stat->blocks =
471 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
472
473
474 switch (inode->i_mode & S_IFMT) {
475 case S_IFBLK:
476 case S_IFCHR:
477 stat->blksize = BLKDEV_IOSIZE;
478 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
479 sysv_minor(ip->i_df.if_u2.if_rdev));
480 break;
481 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100482 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000483 /*
484 * If the file blocks are being allocated from a
485 * realtime volume, then return the inode's realtime
486 * extent size or the realtime volume's extent size.
487 */
488 stat->blksize =
489 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
490 } else
491 stat->blksize = xfs_preferred_iosize(mp);
492 stat->rdev = 0;
493 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000494 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000495
496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100500xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct dentry *dentry,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000502 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +1100504 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Eric Sandeenf35642e2008-11-28 14:23:35 +1100507#define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
508
509/*
510 * Call fiemap helper to fill in user data.
511 * Returns positive errors to xfs_getbmap.
512 */
513STATIC int
514xfs_fiemap_format(
515 void **arg,
516 struct getbmapx *bmv,
517 int *full)
518{
519 int error;
520 struct fiemap_extent_info *fieinfo = *arg;
521 u32 fiemap_flags = 0;
522 u64 logical, physical, length;
523
524 /* Do nothing for a hole */
525 if (bmv->bmv_block == -1LL)
526 return 0;
527
528 logical = BBTOB(bmv->bmv_offset);
529 physical = BBTOB(bmv->bmv_block);
530 length = BBTOB(bmv->bmv_length);
531
532 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
533 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
534 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
535 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
536 physical = 0; /* no block yet */
537 }
538 if (bmv->bmv_oflags & BMV_OF_LAST)
539 fiemap_flags |= FIEMAP_EXTENT_LAST;
540
541 error = fiemap_fill_next_extent(fieinfo, logical, physical,
542 length, fiemap_flags);
543 if (error > 0) {
544 error = 0;
545 *full = 1; /* user array now full */
546 }
547
548 return -error;
549}
550
551STATIC int
552xfs_vn_fiemap(
553 struct inode *inode,
554 struct fiemap_extent_info *fieinfo,
555 u64 start,
556 u64 length)
557{
558 xfs_inode_t *ip = XFS_I(inode);
559 struct getbmapx bm;
560 int error;
561
562 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
563 if (error)
564 return error;
565
566 /* Set up bmap header for xfs internal routine */
567 bm.bmv_offset = BTOBB(start);
568 /* Special case for whole file */
569 if (length == FIEMAP_MAX_OFFSET)
570 bm.bmv_length = -1LL;
571 else
572 bm.bmv_length = BTOBB(length);
573
Eric Sandeen97db39a2009-07-26 21:52:01 -0500574 /* We add one because in getbmap world count includes the header */
Tao Ma2d1ff3c2010-04-29 15:13:56 +1000575 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
576 fieinfo->fi_extents_max + 1;
577 bm.bmv_count = min_t(__s32, bm.bmv_count,
578 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
Tao Ma9af25462010-08-30 02:44:03 +0000579 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
Eric Sandeenf35642e2008-11-28 14:23:35 +1100580 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
581 bm.bmv_iflags |= BMV_IF_ATTRFORK;
582 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
583 bm.bmv_iflags |= BMV_IF_DELALLOC;
584
585 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
586 if (error)
587 return -error;
588
589 return 0;
590}
591
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000592static const struct inode_operations xfs_inode_operations = {
Linus Torvalds18f4c642009-08-28 12:29:03 -0700593 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100594 .getattr = xfs_vn_getattr,
595 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000596 .setxattr = generic_setxattr,
597 .getxattr = generic_getxattr,
598 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100599 .listxattr = xfs_vn_listxattr,
Eric Sandeenf35642e2008-11-28 14:23:35 +1100600 .fiemap = xfs_vn_fiemap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000603static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100604 .create = xfs_vn_create,
605 .lookup = xfs_vn_lookup,
606 .link = xfs_vn_link,
607 .unlink = xfs_vn_unlink,
608 .symlink = xfs_vn_symlink,
609 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000610 /*
611 * Yes, XFS uses the same method for rmdir and unlink.
612 *
613 * There are some subtile differences deeper in the code,
614 * but we use S_ISDIR to check for those.
615 */
616 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100617 .mknod = xfs_vn_mknod,
618 .rename = xfs_vn_rename,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700619 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100620 .getattr = xfs_vn_getattr,
621 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000622 .setxattr = generic_setxattr,
623 .getxattr = generic_getxattr,
624 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100625 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626};
627
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000628static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +1000629 .create = xfs_vn_create,
630 .lookup = xfs_vn_ci_lookup,
631 .link = xfs_vn_link,
632 .unlink = xfs_vn_unlink,
633 .symlink = xfs_vn_symlink,
634 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000635 /*
636 * Yes, XFS uses the same method for rmdir and unlink.
637 *
638 * There are some subtile differences deeper in the code,
639 * but we use S_ISDIR to check for those.
640 */
641 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000642 .mknod = xfs_vn_mknod,
643 .rename = xfs_vn_rename,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700644 .check_acl = xfs_check_acl,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000645 .getattr = xfs_vn_getattr,
646 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000647 .setxattr = generic_setxattr,
648 .getxattr = generic_getxattr,
649 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000650 .listxattr = xfs_vn_listxattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000651};
652
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000653static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100655 .follow_link = xfs_vn_follow_link,
656 .put_link = xfs_vn_put_link,
Linus Torvalds18f4c642009-08-28 12:29:03 -0700657 .check_acl = xfs_check_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +1100658 .getattr = xfs_vn_getattr,
659 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000660 .setxattr = generic_setxattr,
661 .getxattr = generic_getxattr,
662 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100663 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664};
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000665
666STATIC void
667xfs_diflags_to_iflags(
668 struct inode *inode,
669 struct xfs_inode *ip)
670{
671 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
672 inode->i_flags |= S_IMMUTABLE;
673 else
674 inode->i_flags &= ~S_IMMUTABLE;
675 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
676 inode->i_flags |= S_APPEND;
677 else
678 inode->i_flags &= ~S_APPEND;
679 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
680 inode->i_flags |= S_SYNC;
681 else
682 inode->i_flags &= ~S_SYNC;
683 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
684 inode->i_flags |= S_NOATIME;
685 else
686 inode->i_flags &= ~S_NOATIME;
687}
688
689/*
690 * Initialize the Linux inode, set up the operation vectors and
691 * unlock the inode.
692 *
693 * When reading existing inodes from disk this is called directly
694 * from xfs_iget, when creating a new inode it is called from
695 * xfs_ialloc after setting up the inode.
David Chinnerbf904242008-10-30 17:36:14 +1100696 *
697 * We are always called with an uninitialised linux inode here.
698 * We need to initialise the necessary fields and take a reference
699 * on it.
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000700 */
701void
702xfs_setup_inode(
703 struct xfs_inode *ip)
704{
David Chinnerbf904242008-10-30 17:36:14 +1100705 struct inode *inode = &ip->i_vnode;
706
707 inode->i_ino = ip->i_ino;
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100708 inode->i_state = I_NEW;
Christoph Hellwig646ec462010-10-23 07:15:32 -0400709
710 inode_sb_list_add(inode);
Christoph Hellwigc6f6cd02010-11-06 11:43:08 +0000711 /* make the inode look hashed for the writeback code */
712 hlist_add_fake(&inode->i_hash);
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000713
714 inode->i_mode = ip->i_d.di_mode;
715 inode->i_nlink = ip->i_d.di_nlink;
716 inode->i_uid = ip->i_d.di_uid;
717 inode->i_gid = ip->i_d.di_gid;
718
719 switch (inode->i_mode & S_IFMT) {
720 case S_IFBLK:
721 case S_IFCHR:
722 inode->i_rdev =
723 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
724 sysv_minor(ip->i_df.if_u2.if_rdev));
725 break;
726 default:
727 inode->i_rdev = 0;
728 break;
729 }
730
731 inode->i_generation = ip->i_d.di_gen;
732 i_size_write(inode, ip->i_d.di_size);
733 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
734 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
735 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
736 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
737 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
738 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
739 xfs_diflags_to_iflags(inode, ip);
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000740
741 switch (inode->i_mode & S_IFMT) {
742 case S_IFREG:
743 inode->i_op = &xfs_inode_operations;
744 inode->i_fop = &xfs_file_operations;
745 inode->i_mapping->a_ops = &xfs_address_space_operations;
746 break;
747 case S_IFDIR:
748 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
749 inode->i_op = &xfs_dir_ci_inode_operations;
750 else
751 inode->i_op = &xfs_dir_inode_operations;
752 inode->i_fop = &xfs_dir_file_operations;
753 break;
754 case S_IFLNK:
755 inode->i_op = &xfs_symlink_inode_operations;
756 if (!(ip->i_df.if_flags & XFS_IFINLINE))
757 inode->i_mapping->a_ops = &xfs_address_space_operations;
758 break;
759 default:
760 inode->i_op = &xfs_inode_operations;
761 init_special_inode(inode, inode->i_mode, inode->i_rdev);
762 break;
763 }
764
765 xfs_iflags_clear(ip, XFS_INEW);
766 barrier();
767
768 unlock_new_inode(inode);
769}