blob: d6947f818f290f7deae6fbe8d2d652c2d9eef955 [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
Christoph Hellwigaf048192008-03-06 13:46:43 +110067 if (inode) {
68 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 Chinner5d51eff2007-11-23 16:29:18 +110074 * If the linux inode exists, mark it dirty.
75 * 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
Christoph Hellwigaf048192008-03-06 13:46:43 +110084 if (inode)
85 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;
100
Nathan Scott4aeb6642005-11-02 11:43:58 +1100101 nanotime(&tv);
102 if (flags & XFS_ICHGTIME_MOD) {
103 inode->i_mtime = tv;
104 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
105 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
106 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100107 if (flags & XFS_ICHGTIME_CHG) {
108 inode->i_ctime = tv;
109 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
110 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
111 }
112
113 /*
114 * We update the i_update_core field _after_ changing
115 * the timestamps in order to coordinate properly with
116 * xfs_iflush() so that we don't lose timestamp updates.
117 * This keeps us from having to hold the inode lock
118 * while doing this. We use the SYNCHRONIZE macro to
119 * ensure that the compiler does not reorder the update
120 * of i_update_core above the timestamp updates above.
121 */
122 SYNCHRONIZE();
123 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100124 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100125 mark_inode_dirty_sync(inode);
126}
127
128/*
129 * Variant on the above which avoids querying the system clock
130 * in situations where we know the Linux inode timestamps have
131 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100132 */
133void
134xfs_ichgtime_fast(
135 xfs_inode_t *ip,
136 struct inode *inode,
137 int flags)
138{
139 timespec_t *tvp;
140
Nathan Scott4aeb6642005-11-02 11:43:58 +1100141 if (flags & XFS_ICHGTIME_MOD) {
142 tvp = &inode->i_mtime;
143 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
144 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
145 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100146 if (flags & XFS_ICHGTIME_CHG) {
147 tvp = &inode->i_ctime;
148 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
149 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
150 }
151
152 /*
153 * We update the i_update_core field _after_ changing
154 * the timestamps in order to coordinate properly with
155 * xfs_iflush() so that we don't lose timestamp updates.
156 * This keeps us from having to hold the inode lock
157 * while doing this. We use the SYNCHRONIZE macro to
158 * ensure that the compiler does not reorder the update
159 * of i_update_core above the timestamp updates above.
160 */
161 SYNCHRONIZE();
162 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100163 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100164 mark_inode_dirty_sync(inode);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Nathan Scott446ada42006-01-11 15:35:44 +1100168 * Hook in SELinux. This is not quite correct yet, what we really need
169 * here (as we do for default ACLs) is a mechanism by which creation of
170 * these attrs can be journalled at inode creation time (along with the
171 * inode, of course, such that log replay can't cause these to be lost).
172 */
173STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100174xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +1100175 struct inode *inode,
Nathan Scott446ada42006-01-11 15:35:44 +1100176 struct inode *dir)
177{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100178 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100179 size_t length;
180 void *value;
181 char *name;
182 int error;
183
Christoph Hellwigaf048192008-03-06 13:46:43 +1100184 error = security_inode_init_security(inode, dir, &name,
185 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100186 if (error) {
187 if (error == -EOPNOTSUPP)
188 return 0;
189 return -error;
190 }
191
Christoph Hellwigaf048192008-03-06 13:46:43 +1100192 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100193 if (!error)
Christoph Hellwigaf048192008-03-06 13:46:43 +1100194 xfs_iflags_set(ip, XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100195
196 kfree(name);
197 kfree(value);
198 return error;
199}
200
Barry Naujok556b8b12008-04-10 12:22:07 +1000201static void
202xfs_dentry_to_name(
203 struct xfs_name *namep,
204 struct dentry *dentry)
205{
206 namep->name = dentry->d_name.name;
207 namep->len = dentry->d_name.len;
208}
209
David Chinner7989cb82007-02-10 18:34:56 +1100210STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100211xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000212 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100213 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000214 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100215{
Barry Naujok556b8b12008-04-10 12:22:07 +1000216 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100217
218 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100219 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100220 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100221 * ENOSPC can hit here, among other things.
222 */
Barry Naujok556b8b12008-04-10 12:22:07 +1000223 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100224
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000225 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100226 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100230xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct inode *dir,
232 struct dentry *dentry,
233 int mode,
234 dev_t rdev)
235{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100236 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100237 struct xfs_inode *ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 xfs_acl_t *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000239 struct xfs_name name;
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000240 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 int error;
242
243 /*
244 * Irix uses Missed'em'V split, but doesn't want to see
245 * the upper 5 bits of (14bit) major.
246 */
Nathan Scott220b5282006-03-14 13:33:36 +1100247 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EINVAL;
249
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100250 if (test_default_acl && test_default_acl(dir)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100251 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100253 }
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100254 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 _ACL_FREE(default_acl);
256 default_acl = NULL;
257 }
258 }
259
Barry Naujok556b8b12008-04-10 12:22:07 +1000260 xfs_dentry_to_name(&name, dentry);
261
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100262 if (IS_POSIXACL(dir) && !default_acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 mode &= ~current->fs->umask;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 switch (mode & S_IFMT) {
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100266 case S_IFCHR:
267 case S_IFBLK:
268 case S_IFIFO:
269 case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000270 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 case S_IFREG:
Barry Naujok556b8b12008-04-10 12:22:07 +1000272 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 break;
274 case S_IFDIR:
Barry Naujok556b8b12008-04-10 12:22:07 +1000275 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 default:
278 error = EINVAL;
279 break;
280 }
281
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100282 if (unlikely(error))
283 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100284
David Chinner01651642008-08-13 15:45:15 +1000285 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100286
287 error = xfs_init_security(inode, dir);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100288 if (unlikely(error))
289 goto out_cleanup_inode;
290
291 if (default_acl) {
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100292 error = _ACL_INHERIT(inode, mode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100293 if (unlikely(error))
294 goto out_cleanup_inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100295 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 _ACL_FREE(default_acl);
297 }
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100300 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100301 return -error;
302
303 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000304 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100305 out_free_acl:
306 if (default_acl)
307 _ACL_FREE(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -error;
309}
310
311STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100312xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct inode *dir,
314 struct dentry *dentry,
315 int mode,
316 struct nameidata *nd)
317{
Nathan Scott416c6d52006-03-14 14:00:51 +1100318 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100322xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct inode *dir,
324 struct dentry *dentry,
325 int mode)
326{
Nathan Scott416c6d52006-03-14 14:00:51 +1100327 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100331xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct inode *dir,
333 struct dentry *dentry,
334 struct nameidata *nd)
335{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100336 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000337 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int error;
339
340 if (dentry->d_name.len >= MAXNAMELEN)
341 return ERR_PTR(-ENAMETOOLONG);
342
Barry Naujok556b8b12008-04-10 12:22:07 +1000343 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000344 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000345 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (unlikely(error != ENOENT))
347 return ERR_PTR(-error);
348 d_add(dentry, NULL);
349 return NULL;
350 }
351
David Chinner01651642008-08-13 15:45:15 +1000352 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Barry Naujok384f3ce2008-05-21 16:58:22 +1000355STATIC struct dentry *
356xfs_vn_ci_lookup(
357 struct inode *dir,
358 struct dentry *dentry,
359 struct nameidata *nd)
360{
361 struct xfs_inode *ip;
362 struct xfs_name xname;
363 struct xfs_name ci_name;
364 struct qstr dname;
365 int error;
366
367 if (dentry->d_name.len >= MAXNAMELEN)
368 return ERR_PTR(-ENAMETOOLONG);
369
370 xfs_dentry_to_name(&xname, dentry);
371 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
372 if (unlikely(error)) {
373 if (unlikely(error != ENOENT))
374 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000375 /*
376 * call d_add(dentry, NULL) here when d_drop_negative_children
377 * is called in xfs_vn_mknod (ie. allow negative dentries
378 * with CI filesystems).
379 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000380 return NULL;
381 }
382
383 /* if exact match, just splice and exit */
384 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000385 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000386
387 /* else case-insensitive match... */
388 dname.name = ci_name.name;
389 dname.len = ci_name.len;
David Chinner01651642008-08-13 15:45:15 +1000390 dentry = d_add_ci(VFS_I(ip), dentry, &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000391 kmem_free(ci_name.name);
392 return dentry;
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100396xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct dentry *old_dentry,
398 struct inode *dir,
399 struct dentry *dentry)
400{
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100401 struct inode *inode; /* inode of guy being linked to */
Barry Naujok556b8b12008-04-10 12:22:07 +1000402 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int error;
404
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100405 inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000406 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100408 igrab(inode);
Barry Naujok556b8b12008-04-10 12:22:07 +1000409 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Nathan Scott97dfd702006-06-27 16:13:46 +1000410 if (unlikely(error)) {
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100411 iput(inode);
412 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100414
415 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100416 d_instantiate(dentry, inode);
417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100421xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct inode *dir,
423 struct dentry *dentry)
424{
Barry Naujok556b8b12008-04-10 12:22:07 +1000425 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int error;
427
Barry Naujok556b8b12008-04-10 12:22:07 +1000428 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Christoph Hellwige5700702008-06-23 13:25:25 +1000430 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
431 if (error)
432 return error;
433
434 /*
435 * With unlink, the VFS makes the dentry "negative": no inode,
436 * but still hashed. This is incompatible with case-insensitive
437 * mode, so invalidate (unhash) the dentry in CI-mode.
438 */
439 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
440 d_invalidate(dentry);
441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100445xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct inode *dir,
447 struct dentry *dentry,
448 const char *symname)
449{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100450 struct inode *inode;
451 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000452 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000454 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000456 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000457 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Barry Naujok556b8b12008-04-10 12:22:07 +1000458 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Barry Naujok556b8b12008-04-10 12:22:07 +1000460 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100461 if (unlikely(error))
462 goto out;
463
David Chinner01651642008-08-13 15:45:15 +1000464 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100465
466 error = xfs_init_security(inode, dir);
467 if (unlikely(error))
468 goto out_cleanup_inode;
469
470 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100471 return 0;
472
473 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000474 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100475 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -error;
477}
478
479STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100480xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct inode *odir,
482 struct dentry *odentry,
483 struct inode *ndir,
484 struct dentry *ndentry)
485{
486 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000487 struct xfs_name oname;
488 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Barry Naujok556b8b12008-04-10 12:22:07 +1000490 xfs_dentry_to_name(&oname, odentry);
491 xfs_dentry_to_name(&nname, ndentry);
492
Christoph Hellwige5700702008-06-23 13:25:25 +1000493 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000494 XFS_I(ndir), &nname, new_inode ?
495 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * careful here - this function can get called recursively, so
500 * we need to be very careful about how much stack we use.
501 * uio is kmalloced for this reason...
502 */
Al Viro008b1502005-08-20 00:17:39 +0100503STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100504xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct dentry *dentry,
506 struct nameidata *nd)
507{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000509 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700511 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000512 if (!link)
513 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000515 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000516 if (unlikely(error))
517 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100520 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000521
522 out_kfree:
523 kfree(link);
524 out_err:
525 nd_set_link(nd, ERR_PTR(error));
526 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Nathan Scottcde410a2005-09-05 11:47:01 +1000529STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100530xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000531 struct dentry *dentry,
532 struct nameidata *nd,
533 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Nathan Scottcde410a2005-09-05 11:47:01 +1000535 char *s = nd_get_link(nd);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (!IS_ERR(s))
538 kfree(s);
539}
540
541#ifdef CONFIG_XFS_POSIX_ACL
542STATIC int
Christoph Hellwig45767582008-02-05 12:13:24 +1100543xfs_check_acl(
544 struct inode *inode,
545 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Christoph Hellwig45767582008-02-05 12:13:24 +1100547 struct xfs_inode *ip = XFS_I(inode);
548 int error;
549
550 xfs_itrace_entry(ip);
551
552 if (XFS_IFORK_Q(ip)) {
553 error = xfs_acl_iaccess(ip, mask, NULL);
554 if (error != -1)
555 return -error;
556 }
557
558 return -EAGAIN;
559}
560
561STATIC int
562xfs_vn_permission(
563 struct inode *inode,
Al Viroe6305c42008-07-15 21:03:57 -0400564 int mask)
Christoph Hellwig45767582008-02-05 12:13:24 +1100565{
566 return generic_permission(inode, mask, xfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100569#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#endif
571
572STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100573xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000574 struct vfsmount *mnt,
575 struct dentry *dentry,
576 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000578 struct inode *inode = dentry->d_inode;
579 struct xfs_inode *ip = XFS_I(inode);
580 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000582 xfs_itrace_entry(ip);
583
584 if (XFS_FORCED_SHUTDOWN(mp))
585 return XFS_ERROR(EIO);
586
587 stat->size = XFS_ISIZE(ip);
588 stat->dev = inode->i_sb->s_dev;
589 stat->mode = ip->i_d.di_mode;
590 stat->nlink = ip->i_d.di_nlink;
591 stat->uid = ip->i_d.di_uid;
592 stat->gid = ip->i_d.di_gid;
593 stat->ino = ip->i_ino;
594#if XFS_BIG_INUMS
595 stat->ino += mp->m_inoadd;
596#endif
597 stat->atime = inode->i_atime;
598 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
599 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
600 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
601 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
602 stat->blocks =
603 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
604
605
606 switch (inode->i_mode & S_IFMT) {
607 case S_IFBLK:
608 case S_IFCHR:
609 stat->blksize = BLKDEV_IOSIZE;
610 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
611 sysv_minor(ip->i_df.if_u2.if_rdev));
612 break;
613 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100614 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000615 /*
616 * If the file blocks are being allocated from a
617 * realtime volume, then return the inode's realtime
618 * extent size or the realtime volume's extent size.
619 */
620 stat->blksize =
621 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
622 } else
623 stat->blksize = xfs_preferred_iosize(mp);
624 stat->rdev = 0;
625 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000626 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000627
628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100632xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct dentry *dentry,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000634 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000636 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
David Chinnerd87dd632008-04-10 12:21:46 +1000639/*
640 * block_truncate_page can return an error, but we can't propagate it
641 * at all here. Leave a complaint + stack trace in the syslog because
642 * this could be bad. If it is bad, we need to propagate the error further.
643 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100645xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct inode *inode)
647{
David Chinnerd87dd632008-04-10 12:21:46 +1000648 int error;
649 error = block_truncate_page(inode->i_mapping, inode->i_size,
650 xfs_get_blocks);
651 WARN_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
David Chinner3ed65262007-11-23 16:29:25 +1100654STATIC long
655xfs_vn_fallocate(
656 struct inode *inode,
657 int mode,
658 loff_t offset,
659 loff_t len)
660{
661 long error;
662 loff_t new_size = 0;
663 xfs_flock64_t bf;
664 xfs_inode_t *ip = XFS_I(inode);
665
666 /* preallocation on directories not yet supported */
667 error = -ENODEV;
668 if (S_ISDIR(inode->i_mode))
669 goto out_error;
670
671 bf.l_whence = 0;
672 bf.l_start = offset;
673 bf.l_len = len;
674
675 xfs_ilock(ip, XFS_IOLOCK_EXCL);
676 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000677 0, NULL, XFS_ATTR_NOLOCK);
David Chinner3ed65262007-11-23 16:29:25 +1100678 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
679 offset + len > i_size_read(inode))
680 new_size = offset + len;
681
682 /* Change file size if needed */
683 if (new_size) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000684 struct iattr iattr;
David Chinner3ed65262007-11-23 16:29:25 +1100685
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000686 iattr.ia_valid = ATTR_SIZE;
687 iattr.ia_size = new_size;
688 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
David Chinner3ed65262007-11-23 16:29:25 +1100689 }
690
691 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
692out_error:
693 return error;
694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000696static const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100697 .permission = xfs_vn_permission,
698 .truncate = xfs_vn_truncate,
699 .getattr = xfs_vn_getattr,
700 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000701 .setxattr = generic_setxattr,
702 .getxattr = generic_getxattr,
703 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100704 .listxattr = xfs_vn_listxattr,
David Chinner3ed65262007-11-23 16:29:25 +1100705 .fallocate = xfs_vn_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706};
707
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000708static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100709 .create = xfs_vn_create,
710 .lookup = xfs_vn_lookup,
711 .link = xfs_vn_link,
712 .unlink = xfs_vn_unlink,
713 .symlink = xfs_vn_symlink,
714 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000715 /*
716 * Yes, XFS uses the same method for rmdir and unlink.
717 *
718 * There are some subtile differences deeper in the code,
719 * but we use S_ISDIR to check for those.
720 */
721 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100722 .mknod = xfs_vn_mknod,
723 .rename = xfs_vn_rename,
724 .permission = xfs_vn_permission,
725 .getattr = xfs_vn_getattr,
726 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000727 .setxattr = generic_setxattr,
728 .getxattr = generic_getxattr,
729 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100730 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731};
732
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000733static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +1000734 .create = xfs_vn_create,
735 .lookup = xfs_vn_ci_lookup,
736 .link = xfs_vn_link,
737 .unlink = xfs_vn_unlink,
738 .symlink = xfs_vn_symlink,
739 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000740 /*
741 * Yes, XFS uses the same method for rmdir and unlink.
742 *
743 * There are some subtile differences deeper in the code,
744 * but we use S_ISDIR to check for those.
745 */
746 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000747 .mknod = xfs_vn_mknod,
748 .rename = xfs_vn_rename,
749 .permission = xfs_vn_permission,
750 .getattr = xfs_vn_getattr,
751 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000752 .setxattr = generic_setxattr,
753 .getxattr = generic_getxattr,
754 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000755 .listxattr = xfs_vn_listxattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000756};
757
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000758static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100760 .follow_link = xfs_vn_follow_link,
761 .put_link = xfs_vn_put_link,
762 .permission = xfs_vn_permission,
763 .getattr = xfs_vn_getattr,
764 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000765 .setxattr = generic_setxattr,
766 .getxattr = generic_getxattr,
767 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100768 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769};
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000770
771STATIC void
772xfs_diflags_to_iflags(
773 struct inode *inode,
774 struct xfs_inode *ip)
775{
776 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
777 inode->i_flags |= S_IMMUTABLE;
778 else
779 inode->i_flags &= ~S_IMMUTABLE;
780 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
781 inode->i_flags |= S_APPEND;
782 else
783 inode->i_flags &= ~S_APPEND;
784 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
785 inode->i_flags |= S_SYNC;
786 else
787 inode->i_flags &= ~S_SYNC;
788 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
789 inode->i_flags |= S_NOATIME;
790 else
791 inode->i_flags &= ~S_NOATIME;
792}
793
794/*
795 * Initialize the Linux inode, set up the operation vectors and
796 * unlock the inode.
797 *
798 * When reading existing inodes from disk this is called directly
799 * from xfs_iget, when creating a new inode it is called from
800 * xfs_ialloc after setting up the inode.
801 */
802void
803xfs_setup_inode(
804 struct xfs_inode *ip)
805{
806 struct inode *inode = ip->i_vnode;
807
808 inode->i_mode = ip->i_d.di_mode;
809 inode->i_nlink = ip->i_d.di_nlink;
810 inode->i_uid = ip->i_d.di_uid;
811 inode->i_gid = ip->i_d.di_gid;
812
813 switch (inode->i_mode & S_IFMT) {
814 case S_IFBLK:
815 case S_IFCHR:
816 inode->i_rdev =
817 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
818 sysv_minor(ip->i_df.if_u2.if_rdev));
819 break;
820 default:
821 inode->i_rdev = 0;
822 break;
823 }
824
825 inode->i_generation = ip->i_d.di_gen;
826 i_size_write(inode, ip->i_d.di_size);
827 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
828 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
829 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
830 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
831 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
832 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
833 xfs_diflags_to_iflags(inode, ip);
834 xfs_iflags_clear(ip, XFS_IMODIFIED);
835
836 switch (inode->i_mode & S_IFMT) {
837 case S_IFREG:
838 inode->i_op = &xfs_inode_operations;
839 inode->i_fop = &xfs_file_operations;
840 inode->i_mapping->a_ops = &xfs_address_space_operations;
841 break;
842 case S_IFDIR:
843 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
844 inode->i_op = &xfs_dir_ci_inode_operations;
845 else
846 inode->i_op = &xfs_dir_inode_operations;
847 inode->i_fop = &xfs_dir_file_operations;
848 break;
849 case S_IFLNK:
850 inode->i_op = &xfs_symlink_inode_operations;
851 if (!(ip->i_df.if_flags & XFS_IFINLINE))
852 inode->i_mapping->a_ops = &xfs_address_space_operations;
853 break;
854 default:
855 inode->i_op = &xfs_inode_operations;
856 init_special_inode(inode, inode->i_mode, inode->i_rdev);
857 break;
858 }
859
860 xfs_iflags_clear(ip, XFS_INEW);
861 barrier();
862
863 unlock_new_inode(inode);
864}