blob: 37bb1012aff13d365ae06c672d9230db763bec02 [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"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_utils.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100049#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080051#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/xattr.h>
53#include <linux/namei.h>
Nathan Scott446ada42006-01-11 15:35:44 +110054#include <linux/security.h>
David Chinner3ed65262007-11-23 16:29:25 +110055#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Nathan Scott4aeb6642005-11-02 11:43:58 +110057/*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110058 * Bring the atime in the XFS inode uptodate.
59 * Used before logging the inode to disk or when the Linux inode goes away.
60 */
61void
62xfs_synchronize_atime(
63 xfs_inode_t *ip)
64{
David Chinner01651642008-08-13 15:45:15 +100065 struct inode *inode = VFS_I(ip);
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110066
David Chinnerbf904242008-10-30 17:36:14 +110067 if (!(inode->i_state & I_CLEAR)) {
Christoph Hellwigaf048192008-03-06 13:46:43 +110068 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
69 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110070 }
71}
72
73/*
David Chinnerbf904242008-10-30 17:36:14 +110074 * If the linux inode is valid, mark it dirty.
David Chinner5d51eff2007-11-23 16:29:18 +110075 * Used when commiting a dirty inode into a transaction so that
76 * the inode will get written back by the linux code
77 */
78void
79xfs_mark_inode_dirty_sync(
80 xfs_inode_t *ip)
81{
David Chinner01651642008-08-13 15:45:15 +100082 struct inode *inode = VFS_I(ip);
David Chinner5d51eff2007-11-23 16:29:18 +110083
David Chinnerbf904242008-10-30 17:36:14 +110084 if (!(inode->i_state & (I_WILL_FREE|I_FREEING|I_CLEAR)))
Christoph Hellwigaf048192008-03-06 13:46:43 +110085 mark_inode_dirty_sync(inode);
David Chinner5d51eff2007-11-23 16:29:18 +110086}
87
88/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110089 * Change the requested timestamp in the given inode.
90 * We don't lock across timestamp updates, and we don't log them but
91 * we do record the fact that there is dirty information in core.
Nathan Scott4aeb6642005-11-02 11:43:58 +110092 */
93void
94xfs_ichgtime(
95 xfs_inode_t *ip,
96 int flags)
97{
David Chinnere4f75292008-08-13 16:00:45 +100098 struct inode *inode = VFS_I(ip);
Nathan Scott4aeb6642005-11-02 11:43:58 +110099 timespec_t tv;
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000100 int sync_it = 0;
Nathan Scott4aeb6642005-11-02 11:43:58 +1100101
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000102 tv = current_fs_time(inode->i_sb);
103
104 if ((flags & XFS_ICHGTIME_MOD) &&
105 !timespec_equal(&inode->i_mtime, &tv)) {
Nathan Scott4aeb6642005-11-02 11:43:58 +1100106 inode->i_mtime = tv;
107 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
108 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000109 sync_it = 1;
Nathan Scott4aeb6642005-11-02 11:43:58 +1100110 }
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000111 if ((flags & XFS_ICHGTIME_CHG) &&
112 !timespec_equal(&inode->i_ctime, &tv)) {
Nathan Scott4aeb6642005-11-02 11:43:58 +1100113 inode->i_ctime = tv;
114 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
115 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000116 sync_it = 1;
Nathan Scott4aeb6642005-11-02 11:43:58 +1100117 }
118
119 /*
120 * We update the i_update_core field _after_ changing
121 * the timestamps in order to coordinate properly with
122 * xfs_iflush() so that we don't lose timestamp updates.
123 * This keeps us from having to hold the inode lock
124 * while doing this. We use the SYNCHRONIZE macro to
125 * ensure that the compiler does not reorder the update
126 * of i_update_core above the timestamp updates above.
127 */
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000128 if (sync_it) {
129 SYNCHRONIZE();
130 ip->i_update_core = 1;
David Chinner94b97e32008-10-30 17:21:30 +1100131 xfs_mark_inode_dirty_sync(ip);
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000132 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100133}
134
135/*
Nathan Scott446ada42006-01-11 15:35:44 +1100136 * Hook in SELinux. This is not quite correct yet, what we really need
137 * here (as we do for default ACLs) is a mechanism by which creation of
138 * these attrs can be journalled at inode creation time (along with the
139 * inode, of course, such that log replay can't cause these to be lost).
140 */
141STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100142xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +1100143 struct inode *inode,
Nathan Scott446ada42006-01-11 15:35:44 +1100144 struct inode *dir)
145{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100146 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100147 size_t length;
148 void *value;
149 char *name;
150 int error;
151
Christoph Hellwigaf048192008-03-06 13:46:43 +1100152 error = security_inode_init_security(inode, dir, &name,
153 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100154 if (error) {
155 if (error == -EOPNOTSUPP)
156 return 0;
157 return -error;
158 }
159
Christoph Hellwigaf048192008-03-06 13:46:43 +1100160 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100161 if (!error)
Christoph Hellwigaf048192008-03-06 13:46:43 +1100162 xfs_iflags_set(ip, XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100163
164 kfree(name);
165 kfree(value);
166 return error;
167}
168
Barry Naujok556b8b12008-04-10 12:22:07 +1000169static void
170xfs_dentry_to_name(
171 struct xfs_name *namep,
172 struct dentry *dentry)
173{
174 namep->name = dentry->d_name.name;
175 namep->len = dentry->d_name.len;
176}
177
David Chinner7989cb82007-02-10 18:34:56 +1100178STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100179xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000180 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100181 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000182 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100183{
Barry Naujok556b8b12008-04-10 12:22:07 +1000184 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100185
186 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100187 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100188 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100189 * ENOSPC can hit here, among other things.
190 */
Barry Naujok556b8b12008-04-10 12:22:07 +1000191 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100192
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000193 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100194 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100198xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct inode *dir,
200 struct dentry *dentry,
201 int mode,
202 dev_t rdev)
203{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100204 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100205 struct xfs_inode *ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 xfs_acl_t *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000207 struct xfs_name name;
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000208 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int error;
210
211 /*
212 * Irix uses Missed'em'V split, but doesn't want to see
213 * the upper 5 bits of (14bit) major.
214 */
Nathan Scott220b5282006-03-14 13:33:36 +1100215 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -EINVAL;
217
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100218 if (test_default_acl && test_default_acl(dir)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100219 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100221 }
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100222 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 _ACL_FREE(default_acl);
224 default_acl = NULL;
225 }
226 }
227
Barry Naujok556b8b12008-04-10 12:22:07 +1000228 xfs_dentry_to_name(&name, dentry);
229
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100230 if (IS_POSIXACL(dir) && !default_acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mode &= ~current->fs->umask;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 switch (mode & S_IFMT) {
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100234 case S_IFCHR:
235 case S_IFBLK:
236 case S_IFIFO:
237 case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000238 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 case S_IFREG:
Barry Naujok556b8b12008-04-10 12:22:07 +1000240 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 break;
242 case S_IFDIR:
Barry Naujok556b8b12008-04-10 12:22:07 +1000243 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245 default:
246 error = EINVAL;
247 break;
248 }
249
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100250 if (unlikely(error))
251 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100252
David Chinner01651642008-08-13 15:45:15 +1000253 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100254
255 error = xfs_init_security(inode, dir);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100256 if (unlikely(error))
257 goto out_cleanup_inode;
258
259 if (default_acl) {
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100260 error = _ACL_INHERIT(inode, mode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100261 if (unlikely(error))
262 goto out_cleanup_inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100263 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 _ACL_FREE(default_acl);
265 }
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100268 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100269 return -error;
270
271 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000272 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100273 out_free_acl:
274 if (default_acl)
275 _ACL_FREE(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -error;
277}
278
279STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100280xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct inode *dir,
282 struct dentry *dentry,
283 int mode,
284 struct nameidata *nd)
285{
Nathan Scott416c6d52006-03-14 14:00:51 +1100286 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100290xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct inode *dir,
292 struct dentry *dentry,
293 int mode)
294{
Nathan Scott416c6d52006-03-14 14:00:51 +1100295 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100299xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct inode *dir,
301 struct dentry *dentry,
302 struct nameidata *nd)
303{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100304 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000305 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int error;
307
308 if (dentry->d_name.len >= MAXNAMELEN)
309 return ERR_PTR(-ENAMETOOLONG);
310
Barry Naujok556b8b12008-04-10 12:22:07 +1000311 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000312 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000313 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (unlikely(error != ENOENT))
315 return ERR_PTR(-error);
316 d_add(dentry, NULL);
317 return NULL;
318 }
319
David Chinner01651642008-08-13 15:45:15 +1000320 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Barry Naujok384f3ce2008-05-21 16:58:22 +1000323STATIC struct dentry *
324xfs_vn_ci_lookup(
325 struct inode *dir,
326 struct dentry *dentry,
327 struct nameidata *nd)
328{
329 struct xfs_inode *ip;
330 struct xfs_name xname;
331 struct xfs_name ci_name;
332 struct qstr dname;
333 int error;
334
335 if (dentry->d_name.len >= MAXNAMELEN)
336 return ERR_PTR(-ENAMETOOLONG);
337
338 xfs_dentry_to_name(&xname, dentry);
339 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
340 if (unlikely(error)) {
341 if (unlikely(error != ENOENT))
342 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000343 /*
344 * call d_add(dentry, NULL) here when d_drop_negative_children
345 * is called in xfs_vn_mknod (ie. allow negative dentries
346 * with CI filesystems).
347 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000348 return NULL;
349 }
350
351 /* if exact match, just splice and exit */
352 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000353 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000354
355 /* else case-insensitive match... */
356 dname.name = ci_name.name;
357 dname.len = ci_name.len;
Christoph Hellwige45b5902008-08-07 23:49:07 +0200358 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000359 kmem_free(ci_name.name);
360 return dentry;
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100364xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct dentry *old_dentry,
366 struct inode *dir,
367 struct dentry *dentry)
368{
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100369 struct inode *inode; /* inode of guy being linked to */
Barry Naujok556b8b12008-04-10 12:22:07 +1000370 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int error;
372
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100373 inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000374 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100376 igrab(inode);
Barry Naujok556b8b12008-04-10 12:22:07 +1000377 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Nathan Scott97dfd702006-06-27 16:13:46 +1000378 if (unlikely(error)) {
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100379 iput(inode);
380 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100382
383 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100384 d_instantiate(dentry, inode);
385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100389xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct inode *dir,
391 struct dentry *dentry)
392{
Barry Naujok556b8b12008-04-10 12:22:07 +1000393 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int error;
395
Barry Naujok556b8b12008-04-10 12:22:07 +1000396 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Christoph Hellwige5700702008-06-23 13:25:25 +1000398 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
399 if (error)
400 return error;
401
402 /*
403 * With unlink, the VFS makes the dentry "negative": no inode,
404 * but still hashed. This is incompatible with case-insensitive
405 * mode, so invalidate (unhash) the dentry in CI-mode.
406 */
407 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
408 d_invalidate(dentry);
409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100413xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct inode *dir,
415 struct dentry *dentry,
416 const char *symname)
417{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100418 struct inode *inode;
419 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000420 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000422 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000424 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000425 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Barry Naujok556b8b12008-04-10 12:22:07 +1000426 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Barry Naujok556b8b12008-04-10 12:22:07 +1000428 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100429 if (unlikely(error))
430 goto out;
431
David Chinner01651642008-08-13 15:45:15 +1000432 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100433
434 error = xfs_init_security(inode, dir);
435 if (unlikely(error))
436 goto out_cleanup_inode;
437
438 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100439 return 0;
440
441 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000442 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100443 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -error;
445}
446
447STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100448xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct inode *odir,
450 struct dentry *odentry,
451 struct inode *ndir,
452 struct dentry *ndentry)
453{
454 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000455 struct xfs_name oname;
456 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Barry Naujok556b8b12008-04-10 12:22:07 +1000458 xfs_dentry_to_name(&oname, odentry);
459 xfs_dentry_to_name(&nname, ndentry);
460
Christoph Hellwige5700702008-06-23 13:25:25 +1000461 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000462 XFS_I(ndir), &nname, new_inode ?
463 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * careful here - this function can get called recursively, so
468 * we need to be very careful about how much stack we use.
469 * uio is kmalloced for this reason...
470 */
Al Viro008b1502005-08-20 00:17:39 +0100471STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100472xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct dentry *dentry,
474 struct nameidata *nd)
475{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000477 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700479 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000480 if (!link)
481 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000483 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000484 if (unlikely(error))
485 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100488 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000489
490 out_kfree:
491 kfree(link);
492 out_err:
493 nd_set_link(nd, ERR_PTR(error));
494 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Nathan Scottcde410a2005-09-05 11:47:01 +1000497STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100498xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000499 struct dentry *dentry,
500 struct nameidata *nd,
501 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Nathan Scottcde410a2005-09-05 11:47:01 +1000503 char *s = nd_get_link(nd);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!IS_ERR(s))
506 kfree(s);
507}
508
509#ifdef CONFIG_XFS_POSIX_ACL
510STATIC int
Christoph Hellwig45767582008-02-05 12:13:24 +1100511xfs_check_acl(
512 struct inode *inode,
513 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Christoph Hellwig45767582008-02-05 12:13:24 +1100515 struct xfs_inode *ip = XFS_I(inode);
516 int error;
517
518 xfs_itrace_entry(ip);
519
520 if (XFS_IFORK_Q(ip)) {
521 error = xfs_acl_iaccess(ip, mask, NULL);
522 if (error != -1)
523 return -error;
524 }
525
526 return -EAGAIN;
527}
528
529STATIC int
530xfs_vn_permission(
531 struct inode *inode,
Al Viroe6305c42008-07-15 21:03:57 -0400532 int mask)
Christoph Hellwig45767582008-02-05 12:13:24 +1100533{
534 return generic_permission(inode, mask, xfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100537#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#endif
539
540STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100541xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000542 struct vfsmount *mnt,
543 struct dentry *dentry,
544 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000546 struct inode *inode = dentry->d_inode;
547 struct xfs_inode *ip = XFS_I(inode);
548 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000550 xfs_itrace_entry(ip);
551
552 if (XFS_FORCED_SHUTDOWN(mp))
553 return XFS_ERROR(EIO);
554
555 stat->size = XFS_ISIZE(ip);
556 stat->dev = inode->i_sb->s_dev;
557 stat->mode = ip->i_d.di_mode;
558 stat->nlink = ip->i_d.di_nlink;
559 stat->uid = ip->i_d.di_uid;
560 stat->gid = ip->i_d.di_gid;
561 stat->ino = ip->i_ino;
562#if XFS_BIG_INUMS
563 stat->ino += mp->m_inoadd;
564#endif
565 stat->atime = inode->i_atime;
566 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
567 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
568 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
569 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
570 stat->blocks =
571 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
572
573
574 switch (inode->i_mode & S_IFMT) {
575 case S_IFBLK:
576 case S_IFCHR:
577 stat->blksize = BLKDEV_IOSIZE;
578 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
579 sysv_minor(ip->i_df.if_u2.if_rdev));
580 break;
581 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100582 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000583 /*
584 * If the file blocks are being allocated from a
585 * realtime volume, then return the inode's realtime
586 * extent size or the realtime volume's extent size.
587 */
588 stat->blksize =
589 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
590 } else
591 stat->blksize = xfs_preferred_iosize(mp);
592 stat->rdev = 0;
593 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000594 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000595
596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100600xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct dentry *dentry,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000602 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000604 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
David Chinnerd87dd632008-04-10 12:21:46 +1000607/*
608 * block_truncate_page can return an error, but we can't propagate it
609 * at all here. Leave a complaint + stack trace in the syslog because
610 * this could be bad. If it is bad, we need to propagate the error further.
611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100613xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct inode *inode)
615{
David Chinnerd87dd632008-04-10 12:21:46 +1000616 int error;
617 error = block_truncate_page(inode->i_mapping, inode->i_size,
618 xfs_get_blocks);
619 WARN_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
David Chinner3ed65262007-11-23 16:29:25 +1100622STATIC long
623xfs_vn_fallocate(
624 struct inode *inode,
625 int mode,
626 loff_t offset,
627 loff_t len)
628{
629 long error;
630 loff_t new_size = 0;
631 xfs_flock64_t bf;
632 xfs_inode_t *ip = XFS_I(inode);
633
634 /* preallocation on directories not yet supported */
635 error = -ENODEV;
636 if (S_ISDIR(inode->i_mode))
637 goto out_error;
638
639 bf.l_whence = 0;
640 bf.l_start = offset;
641 bf.l_len = len;
642
643 xfs_ilock(ip, XFS_IOLOCK_EXCL);
644 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000645 0, NULL, XFS_ATTR_NOLOCK);
David Chinner3ed65262007-11-23 16:29:25 +1100646 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
647 offset + len > i_size_read(inode))
648 new_size = offset + len;
649
650 /* Change file size if needed */
651 if (new_size) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000652 struct iattr iattr;
David Chinner3ed65262007-11-23 16:29:25 +1100653
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000654 iattr.ia_valid = ATTR_SIZE;
655 iattr.ia_size = new_size;
656 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
David Chinner3ed65262007-11-23 16:29:25 +1100657 }
658
659 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
660out_error:
661 return error;
662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000664static const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100665 .permission = xfs_vn_permission,
666 .truncate = xfs_vn_truncate,
667 .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,
David Chinner3ed65262007-11-23 16:29:25 +1100673 .fallocate = xfs_vn_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674};
675
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000676static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100677 .create = xfs_vn_create,
678 .lookup = xfs_vn_lookup,
679 .link = xfs_vn_link,
680 .unlink = xfs_vn_unlink,
681 .symlink = xfs_vn_symlink,
682 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000683 /*
684 * Yes, XFS uses the same method for rmdir and unlink.
685 *
686 * There are some subtile differences deeper in the code,
687 * but we use S_ISDIR to check for those.
688 */
689 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100690 .mknod = xfs_vn_mknod,
691 .rename = xfs_vn_rename,
692 .permission = xfs_vn_permission,
693 .getattr = xfs_vn_getattr,
694 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000695 .setxattr = generic_setxattr,
696 .getxattr = generic_getxattr,
697 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100698 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000701static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +1000702 .create = xfs_vn_create,
703 .lookup = xfs_vn_ci_lookup,
704 .link = xfs_vn_link,
705 .unlink = xfs_vn_unlink,
706 .symlink = xfs_vn_symlink,
707 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000708 /*
709 * Yes, XFS uses the same method for rmdir and unlink.
710 *
711 * There are some subtile differences deeper in the code,
712 * but we use S_ISDIR to check for those.
713 */
714 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000715 .mknod = xfs_vn_mknod,
716 .rename = xfs_vn_rename,
717 .permission = xfs_vn_permission,
718 .getattr = xfs_vn_getattr,
719 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000720 .setxattr = generic_setxattr,
721 .getxattr = generic_getxattr,
722 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000723 .listxattr = xfs_vn_listxattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000724};
725
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000726static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100728 .follow_link = xfs_vn_follow_link,
729 .put_link = xfs_vn_put_link,
730 .permission = xfs_vn_permission,
731 .getattr = xfs_vn_getattr,
732 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000733 .setxattr = generic_setxattr,
734 .getxattr = generic_getxattr,
735 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100736 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737};
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000738
739STATIC void
740xfs_diflags_to_iflags(
741 struct inode *inode,
742 struct xfs_inode *ip)
743{
744 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
745 inode->i_flags |= S_IMMUTABLE;
746 else
747 inode->i_flags &= ~S_IMMUTABLE;
748 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
749 inode->i_flags |= S_APPEND;
750 else
751 inode->i_flags &= ~S_APPEND;
752 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
753 inode->i_flags |= S_SYNC;
754 else
755 inode->i_flags &= ~S_SYNC;
756 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
757 inode->i_flags |= S_NOATIME;
758 else
759 inode->i_flags &= ~S_NOATIME;
760}
761
762/*
763 * Initialize the Linux inode, set up the operation vectors and
764 * unlock the inode.
765 *
766 * When reading existing inodes from disk this is called directly
767 * from xfs_iget, when creating a new inode it is called from
768 * xfs_ialloc after setting up the inode.
David Chinnerbf904242008-10-30 17:36:14 +1100769 *
770 * We are always called with an uninitialised linux inode here.
771 * We need to initialise the necessary fields and take a reference
772 * on it.
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000773 */
774void
775xfs_setup_inode(
776 struct xfs_inode *ip)
777{
David Chinnerbf904242008-10-30 17:36:14 +1100778 struct inode *inode = &ip->i_vnode;
779
780 inode->i_ino = ip->i_ino;
781 inode->i_state = I_NEW|I_LOCK;
782 inode_add_to_lists(ip->i_mount->m_super, inode);
783 ASSERT(atomic_read(&inode->i_count) == 1);
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000784
785 inode->i_mode = ip->i_d.di_mode;
786 inode->i_nlink = ip->i_d.di_nlink;
787 inode->i_uid = ip->i_d.di_uid;
788 inode->i_gid = ip->i_d.di_gid;
789
790 switch (inode->i_mode & S_IFMT) {
791 case S_IFBLK:
792 case S_IFCHR:
793 inode->i_rdev =
794 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
795 sysv_minor(ip->i_df.if_u2.if_rdev));
796 break;
797 default:
798 inode->i_rdev = 0;
799 break;
800 }
801
802 inode->i_generation = ip->i_d.di_gen;
803 i_size_write(inode, ip->i_d.di_size);
804 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
805 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
806 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
807 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
808 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
809 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
810 xfs_diflags_to_iflags(inode, ip);
811 xfs_iflags_clear(ip, XFS_IMODIFIED);
812
813 switch (inode->i_mode & S_IFMT) {
814 case S_IFREG:
815 inode->i_op = &xfs_inode_operations;
816 inode->i_fop = &xfs_file_operations;
817 inode->i_mapping->a_ops = &xfs_address_space_operations;
818 break;
819 case S_IFDIR:
820 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
821 inode->i_op = &xfs_dir_ci_inode_operations;
822 else
823 inode->i_op = &xfs_dir_inode_operations;
824 inode->i_fop = &xfs_dir_file_operations;
825 break;
826 case S_IFLNK:
827 inode->i_op = &xfs_symlink_inode_operations;
828 if (!(ip->i_df.if_flags & XFS_IFINLINE))
829 inode->i_mapping->a_ops = &xfs_address_space_operations;
830 break;
831 default:
832 inode->i_op = &xfs_inode_operations;
833 init_special_inode(inode, inode->i_mode, inode->i_rdev);
834 break;
835 }
836
837 xfs_iflags_clear(ip, XFS_INEW);
838 barrier();
839
840 unlock_new_inode(inode);
841}