blob: ace56bd3a5fb49895c16fe61e5a4cc555c837499 [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.
92 *
93 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
94 * with XFS_ICHGTIME_ACC to be sure that access time
95 * update will take. Calling first with XFS_ICHGTIME_ACC
96 * and then XFS_ICHGTIME_MOD may fail to modify the access
97 * timestamp if the filesystem is mounted noacctm.
98 */
99void
100xfs_ichgtime(
101 xfs_inode_t *ip,
102 int flags)
103{
David Chinnere4f75292008-08-13 16:00:45 +1000104 struct inode *inode = VFS_I(ip);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100105 timespec_t tv;
106
Nathan Scott4aeb6642005-11-02 11:43:58 +1100107 nanotime(&tv);
108 if (flags & XFS_ICHGTIME_MOD) {
109 inode->i_mtime = tv;
110 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
111 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
112 }
113 if (flags & XFS_ICHGTIME_ACC) {
114 inode->i_atime = tv;
115 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
116 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
117 }
118 if (flags & XFS_ICHGTIME_CHG) {
119 inode->i_ctime = tv;
120 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
121 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
122 }
123
124 /*
125 * We update the i_update_core field _after_ changing
126 * the timestamps in order to coordinate properly with
127 * xfs_iflush() so that we don't lose timestamp updates.
128 * This keeps us from having to hold the inode lock
129 * while doing this. We use the SYNCHRONIZE macro to
130 * ensure that the compiler does not reorder the update
131 * of i_update_core above the timestamp updates above.
132 */
133 SYNCHRONIZE();
134 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100135 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100136 mark_inode_dirty_sync(inode);
137}
138
139/*
140 * Variant on the above which avoids querying the system clock
141 * in situations where we know the Linux inode timestamps have
142 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100143 */
144void
145xfs_ichgtime_fast(
146 xfs_inode_t *ip,
147 struct inode *inode,
148 int flags)
149{
150 timespec_t *tvp;
151
152 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100153 * Atime updates for read() & friends are handled lazily now, and
154 * explicit updates must go through xfs_ichgtime()
155 */
156 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
157
Nathan Scott4aeb6642005-11-02 11:43:58 +1100158 if (flags & XFS_ICHGTIME_MOD) {
159 tvp = &inode->i_mtime;
160 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
161 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
162 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100163 if (flags & XFS_ICHGTIME_CHG) {
164 tvp = &inode->i_ctime;
165 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
166 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
167 }
168
169 /*
170 * We update the i_update_core field _after_ changing
171 * the timestamps in order to coordinate properly with
172 * xfs_iflush() so that we don't lose timestamp updates.
173 * This keeps us from having to hold the inode lock
174 * while doing this. We use the SYNCHRONIZE macro to
175 * ensure that the compiler does not reorder the update
176 * of i_update_core above the timestamp updates above.
177 */
178 SYNCHRONIZE();
179 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100180 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100181 mark_inode_dirty_sync(inode);
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
Nathan Scott446ada42006-01-11 15:35:44 +1100185 * Hook in SELinux. This is not quite correct yet, what we really need
186 * here (as we do for default ACLs) is a mechanism by which creation of
187 * these attrs can be journalled at inode creation time (along with the
188 * inode, of course, such that log replay can't cause these to be lost).
189 */
190STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100191xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +1100192 struct inode *inode,
Nathan Scott446ada42006-01-11 15:35:44 +1100193 struct inode *dir)
194{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100195 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100196 size_t length;
197 void *value;
198 char *name;
199 int error;
200
Christoph Hellwigaf048192008-03-06 13:46:43 +1100201 error = security_inode_init_security(inode, dir, &name,
202 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100203 if (error) {
204 if (error == -EOPNOTSUPP)
205 return 0;
206 return -error;
207 }
208
Christoph Hellwigaf048192008-03-06 13:46:43 +1100209 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100210 if (!error)
Christoph Hellwigaf048192008-03-06 13:46:43 +1100211 xfs_iflags_set(ip, XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100212
213 kfree(name);
214 kfree(value);
215 return error;
216}
217
Barry Naujok556b8b12008-04-10 12:22:07 +1000218static void
219xfs_dentry_to_name(
220 struct xfs_name *namep,
221 struct dentry *dentry)
222{
223 namep->name = dentry->d_name.name;
224 namep->len = dentry->d_name.len;
225}
226
David Chinner7989cb82007-02-10 18:34:56 +1100227STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100228xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000229 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100230 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000231 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100232{
Barry Naujok556b8b12008-04-10 12:22:07 +1000233 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100234
235 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100236 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100237 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100238 * ENOSPC can hit here, among other things.
239 */
Barry Naujok556b8b12008-04-10 12:22:07 +1000240 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100241
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000242 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100243 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100247xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct inode *dir,
249 struct dentry *dentry,
250 int mode,
251 dev_t rdev)
252{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100253 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100254 struct xfs_inode *ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 xfs_acl_t *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000256 struct xfs_name name;
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000257 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int error;
259
260 /*
261 * Irix uses Missed'em'V split, but doesn't want to see
262 * the upper 5 bits of (14bit) major.
263 */
Nathan Scott220b5282006-03-14 13:33:36 +1100264 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EINVAL;
266
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100267 if (test_default_acl && test_default_acl(dir)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100268 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100270 }
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100271 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 _ACL_FREE(default_acl);
273 default_acl = NULL;
274 }
275 }
276
Barry Naujok556b8b12008-04-10 12:22:07 +1000277 xfs_dentry_to_name(&name, dentry);
278
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100279 if (IS_POSIXACL(dir) && !default_acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 mode &= ~current->fs->umask;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 switch (mode & S_IFMT) {
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100283 case S_IFCHR:
284 case S_IFBLK:
285 case S_IFIFO:
286 case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000287 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case S_IFREG:
Barry Naujok556b8b12008-04-10 12:22:07 +1000289 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 break;
291 case S_IFDIR:
Barry Naujok556b8b12008-04-10 12:22:07 +1000292 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294 default:
295 error = EINVAL;
296 break;
297 }
298
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100299 if (unlikely(error))
300 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100301
David Chinner01651642008-08-13 15:45:15 +1000302 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100303
304 error = xfs_init_security(inode, dir);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100305 if (unlikely(error))
306 goto out_cleanup_inode;
307
308 if (default_acl) {
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100309 error = _ACL_INHERIT(inode, mode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100310 if (unlikely(error))
311 goto out_cleanup_inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100312 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 _ACL_FREE(default_acl);
314 }
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100317 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100318 return -error;
319
320 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000321 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100322 out_free_acl:
323 if (default_acl)
324 _ACL_FREE(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -error;
326}
327
328STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100329xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct inode *dir,
331 struct dentry *dentry,
332 int mode,
333 struct nameidata *nd)
334{
Nathan Scott416c6d52006-03-14 14:00:51 +1100335 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100339xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct inode *dir,
341 struct dentry *dentry,
342 int mode)
343{
Nathan Scott416c6d52006-03-14 14:00:51 +1100344 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100348xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct inode *dir,
350 struct dentry *dentry,
351 struct nameidata *nd)
352{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100353 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000354 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int error;
356
357 if (dentry->d_name.len >= MAXNAMELEN)
358 return ERR_PTR(-ENAMETOOLONG);
359
Barry Naujok556b8b12008-04-10 12:22:07 +1000360 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000361 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000362 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (unlikely(error != ENOENT))
364 return ERR_PTR(-error);
365 d_add(dentry, NULL);
366 return NULL;
367 }
368
David Chinner01651642008-08-13 15:45:15 +1000369 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Barry Naujok384f3ce2008-05-21 16:58:22 +1000372STATIC struct dentry *
373xfs_vn_ci_lookup(
374 struct inode *dir,
375 struct dentry *dentry,
376 struct nameidata *nd)
377{
378 struct xfs_inode *ip;
379 struct xfs_name xname;
380 struct xfs_name ci_name;
381 struct qstr dname;
382 int error;
383
384 if (dentry->d_name.len >= MAXNAMELEN)
385 return ERR_PTR(-ENAMETOOLONG);
386
387 xfs_dentry_to_name(&xname, dentry);
388 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
389 if (unlikely(error)) {
390 if (unlikely(error != ENOENT))
391 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000392 /*
393 * call d_add(dentry, NULL) here when d_drop_negative_children
394 * is called in xfs_vn_mknod (ie. allow negative dentries
395 * with CI filesystems).
396 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000397 return NULL;
398 }
399
400 /* if exact match, just splice and exit */
401 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000402 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000403
404 /* else case-insensitive match... */
405 dname.name = ci_name.name;
406 dname.len = ci_name.len;
David Chinner01651642008-08-13 15:45:15 +1000407 dentry = d_add_ci(VFS_I(ip), dentry, &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000408 kmem_free(ci_name.name);
409 return dentry;
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100413xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct dentry *old_dentry,
415 struct inode *dir,
416 struct dentry *dentry)
417{
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100418 struct inode *inode; /* inode of guy being linked to */
Barry Naujok556b8b12008-04-10 12:22:07 +1000419 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int error;
421
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100422 inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000423 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100425 igrab(inode);
Barry Naujok556b8b12008-04-10 12:22:07 +1000426 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Nathan Scott97dfd702006-06-27 16:13:46 +1000427 if (unlikely(error)) {
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100428 iput(inode);
429 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100431
432 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100433 d_instantiate(dentry, inode);
434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100438xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct inode *dir,
440 struct dentry *dentry)
441{
Barry Naujok556b8b12008-04-10 12:22:07 +1000442 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int error;
444
Barry Naujok556b8b12008-04-10 12:22:07 +1000445 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Christoph Hellwige5700702008-06-23 13:25:25 +1000447 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
448 if (error)
449 return error;
450
451 /*
452 * With unlink, the VFS makes the dentry "negative": no inode,
453 * but still hashed. This is incompatible with case-insensitive
454 * mode, so invalidate (unhash) the dentry in CI-mode.
455 */
456 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
457 d_invalidate(dentry);
458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100462xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct inode *dir,
464 struct dentry *dentry,
465 const char *symname)
466{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100467 struct inode *inode;
468 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000469 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000471 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000473 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000474 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Barry Naujok556b8b12008-04-10 12:22:07 +1000475 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Barry Naujok556b8b12008-04-10 12:22:07 +1000477 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100478 if (unlikely(error))
479 goto out;
480
David Chinner01651642008-08-13 15:45:15 +1000481 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100482
483 error = xfs_init_security(inode, dir);
484 if (unlikely(error))
485 goto out_cleanup_inode;
486
487 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100488 return 0;
489
490 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000491 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100492 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -error;
494}
495
496STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100497xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct inode *odir,
499 struct dentry *odentry,
500 struct inode *ndir,
501 struct dentry *ndentry)
502{
503 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000504 struct xfs_name oname;
505 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Barry Naujok556b8b12008-04-10 12:22:07 +1000507 xfs_dentry_to_name(&oname, odentry);
508 xfs_dentry_to_name(&nname, ndentry);
509
Christoph Hellwige5700702008-06-23 13:25:25 +1000510 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000511 XFS_I(ndir), &nname, new_inode ?
512 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/*
516 * careful here - this function can get called recursively, so
517 * we need to be very careful about how much stack we use.
518 * uio is kmalloced for this reason...
519 */
Al Viro008b1502005-08-20 00:17:39 +0100520STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100521xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct dentry *dentry,
523 struct nameidata *nd)
524{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000526 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700528 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000529 if (!link)
530 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000532 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000533 if (unlikely(error))
534 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100537 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000538
539 out_kfree:
540 kfree(link);
541 out_err:
542 nd_set_link(nd, ERR_PTR(error));
543 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Nathan Scottcde410a2005-09-05 11:47:01 +1000546STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100547xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000548 struct dentry *dentry,
549 struct nameidata *nd,
550 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Nathan Scottcde410a2005-09-05 11:47:01 +1000552 char *s = nd_get_link(nd);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!IS_ERR(s))
555 kfree(s);
556}
557
558#ifdef CONFIG_XFS_POSIX_ACL
559STATIC int
Christoph Hellwig45767582008-02-05 12:13:24 +1100560xfs_check_acl(
561 struct inode *inode,
562 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Christoph Hellwig45767582008-02-05 12:13:24 +1100564 struct xfs_inode *ip = XFS_I(inode);
565 int error;
566
567 xfs_itrace_entry(ip);
568
569 if (XFS_IFORK_Q(ip)) {
570 error = xfs_acl_iaccess(ip, mask, NULL);
571 if (error != -1)
572 return -error;
573 }
574
575 return -EAGAIN;
576}
577
578STATIC int
579xfs_vn_permission(
580 struct inode *inode,
Al Viroe6305c42008-07-15 21:03:57 -0400581 int mask)
Christoph Hellwig45767582008-02-05 12:13:24 +1100582{
583 return generic_permission(inode, mask, xfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100586#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#endif
588
589STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100590xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000591 struct vfsmount *mnt,
592 struct dentry *dentry,
593 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000595 struct inode *inode = dentry->d_inode;
596 struct xfs_inode *ip = XFS_I(inode);
597 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000599 xfs_itrace_entry(ip);
600
601 if (XFS_FORCED_SHUTDOWN(mp))
602 return XFS_ERROR(EIO);
603
604 stat->size = XFS_ISIZE(ip);
605 stat->dev = inode->i_sb->s_dev;
606 stat->mode = ip->i_d.di_mode;
607 stat->nlink = ip->i_d.di_nlink;
608 stat->uid = ip->i_d.di_uid;
609 stat->gid = ip->i_d.di_gid;
610 stat->ino = ip->i_ino;
611#if XFS_BIG_INUMS
612 stat->ino += mp->m_inoadd;
613#endif
614 stat->atime = inode->i_atime;
615 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
616 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
617 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
618 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
619 stat->blocks =
620 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
621
622
623 switch (inode->i_mode & S_IFMT) {
624 case S_IFBLK:
625 case S_IFCHR:
626 stat->blksize = BLKDEV_IOSIZE;
627 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
628 sysv_minor(ip->i_df.if_u2.if_rdev));
629 break;
630 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100631 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000632 /*
633 * If the file blocks are being allocated from a
634 * realtime volume, then return the inode's realtime
635 * extent size or the realtime volume's extent size.
636 */
637 stat->blksize =
638 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
639 } else
640 stat->blksize = xfs_preferred_iosize(mp);
641 stat->rdev = 0;
642 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000643 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000644
645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100649xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct dentry *dentry,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000651 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000653 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
David Chinnerd87dd632008-04-10 12:21:46 +1000656/*
657 * block_truncate_page can return an error, but we can't propagate it
658 * at all here. Leave a complaint + stack trace in the syslog because
659 * this could be bad. If it is bad, we need to propagate the error further.
660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100662xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 struct inode *inode)
664{
David Chinnerd87dd632008-04-10 12:21:46 +1000665 int error;
666 error = block_truncate_page(inode->i_mapping, inode->i_size,
667 xfs_get_blocks);
668 WARN_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
David Chinner3ed65262007-11-23 16:29:25 +1100671STATIC long
672xfs_vn_fallocate(
673 struct inode *inode,
674 int mode,
675 loff_t offset,
676 loff_t len)
677{
678 long error;
679 loff_t new_size = 0;
680 xfs_flock64_t bf;
681 xfs_inode_t *ip = XFS_I(inode);
682
683 /* preallocation on directories not yet supported */
684 error = -ENODEV;
685 if (S_ISDIR(inode->i_mode))
686 goto out_error;
687
688 bf.l_whence = 0;
689 bf.l_start = offset;
690 bf.l_len = len;
691
692 xfs_ilock(ip, XFS_IOLOCK_EXCL);
693 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000694 0, NULL, XFS_ATTR_NOLOCK);
David Chinner3ed65262007-11-23 16:29:25 +1100695 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
696 offset + len > i_size_read(inode))
697 new_size = offset + len;
698
699 /* Change file size if needed */
700 if (new_size) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000701 struct iattr iattr;
David Chinner3ed65262007-11-23 16:29:25 +1100702
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000703 iattr.ia_valid = ATTR_SIZE;
704 iattr.ia_size = new_size;
705 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
David Chinner3ed65262007-11-23 16:29:25 +1100706 }
707
708 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
709out_error:
710 return error;
711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000713static const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100714 .permission = xfs_vn_permission,
715 .truncate = xfs_vn_truncate,
716 .getattr = xfs_vn_getattr,
717 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000718 .setxattr = generic_setxattr,
719 .getxattr = generic_getxattr,
720 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100721 .listxattr = xfs_vn_listxattr,
David Chinner3ed65262007-11-23 16:29:25 +1100722 .fallocate = xfs_vn_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723};
724
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000725static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100726 .create = xfs_vn_create,
727 .lookup = xfs_vn_lookup,
728 .link = xfs_vn_link,
729 .unlink = xfs_vn_unlink,
730 .symlink = xfs_vn_symlink,
731 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000732 /*
733 * Yes, XFS uses the same method for rmdir and unlink.
734 *
735 * There are some subtile differences deeper in the code,
736 * but we use S_ISDIR to check for those.
737 */
738 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100739 .mknod = xfs_vn_mknod,
740 .rename = xfs_vn_rename,
741 .permission = xfs_vn_permission,
742 .getattr = xfs_vn_getattr,
743 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000744 .setxattr = generic_setxattr,
745 .getxattr = generic_getxattr,
746 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100747 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748};
749
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000750static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +1000751 .create = xfs_vn_create,
752 .lookup = xfs_vn_ci_lookup,
753 .link = xfs_vn_link,
754 .unlink = xfs_vn_unlink,
755 .symlink = xfs_vn_symlink,
756 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000757 /*
758 * Yes, XFS uses the same method for rmdir and unlink.
759 *
760 * There are some subtile differences deeper in the code,
761 * but we use S_ISDIR to check for those.
762 */
763 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000764 .mknod = xfs_vn_mknod,
765 .rename = xfs_vn_rename,
766 .permission = xfs_vn_permission,
767 .getattr = xfs_vn_getattr,
768 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000769 .setxattr = generic_setxattr,
770 .getxattr = generic_getxattr,
771 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000772 .listxattr = xfs_vn_listxattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000773};
774
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000775static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100777 .follow_link = xfs_vn_follow_link,
778 .put_link = xfs_vn_put_link,
779 .permission = xfs_vn_permission,
780 .getattr = xfs_vn_getattr,
781 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000782 .setxattr = generic_setxattr,
783 .getxattr = generic_getxattr,
784 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100785 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786};
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000787
788STATIC void
789xfs_diflags_to_iflags(
790 struct inode *inode,
791 struct xfs_inode *ip)
792{
793 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
794 inode->i_flags |= S_IMMUTABLE;
795 else
796 inode->i_flags &= ~S_IMMUTABLE;
797 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
798 inode->i_flags |= S_APPEND;
799 else
800 inode->i_flags &= ~S_APPEND;
801 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
802 inode->i_flags |= S_SYNC;
803 else
804 inode->i_flags &= ~S_SYNC;
805 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
806 inode->i_flags |= S_NOATIME;
807 else
808 inode->i_flags &= ~S_NOATIME;
809}
810
811/*
812 * Initialize the Linux inode, set up the operation vectors and
813 * unlock the inode.
814 *
815 * When reading existing inodes from disk this is called directly
816 * from xfs_iget, when creating a new inode it is called from
817 * xfs_ialloc after setting up the inode.
818 */
819void
820xfs_setup_inode(
821 struct xfs_inode *ip)
822{
823 struct inode *inode = ip->i_vnode;
824
825 inode->i_mode = ip->i_d.di_mode;
826 inode->i_nlink = ip->i_d.di_nlink;
827 inode->i_uid = ip->i_d.di_uid;
828 inode->i_gid = ip->i_d.di_gid;
829
830 switch (inode->i_mode & S_IFMT) {
831 case S_IFBLK:
832 case S_IFCHR:
833 inode->i_rdev =
834 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
835 sysv_minor(ip->i_df.if_u2.if_rdev));
836 break;
837 default:
838 inode->i_rdev = 0;
839 break;
840 }
841
842 inode->i_generation = ip->i_d.di_gen;
843 i_size_write(inode, ip->i_d.di_size);
844 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
845 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
846 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
847 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
848 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
849 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
850 xfs_diflags_to_iflags(inode, ip);
851 xfs_iflags_clear(ip, XFS_IMODIFIED);
852
853 switch (inode->i_mode & S_IFMT) {
854 case S_IFREG:
855 inode->i_op = &xfs_inode_operations;
856 inode->i_fop = &xfs_file_operations;
857 inode->i_mapping->a_ops = &xfs_address_space_operations;
858 break;
859 case S_IFDIR:
860 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
861 inode->i_op = &xfs_dir_ci_inode_operations;
862 else
863 inode->i_op = &xfs_dir_inode_operations;
864 inode->i_fop = &xfs_dir_file_operations;
865 break;
866 case S_IFLNK:
867 inode->i_op = &xfs_symlink_inode_operations;
868 if (!(ip->i_df.if_flags & XFS_IFINLINE))
869 inode->i_mapping->a_ops = &xfs_address_space_operations;
870 break;
871 default:
872 inode->i_op = &xfs_inode_operations;
873 init_special_inode(inode, inode->i_mode, inode->i_rdev);
874 break;
875 }
876
877 xfs_iflags_clear(ip, XFS_INEW);
878 barrier();
879
880 unlock_new_inode(inode);
881}