blob: 0e7ca2155956793bd877afbe4dbc83cd1552996f [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;
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;
Nathan Scott4aeb6642005-11-02 11:43:58 +1100131 mark_inode_dirty_sync(inode);
Christoph Hellwig8e5975c2008-08-13 16:45:13 +1000132 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100133}
134
135/*
136 * Variant on the above which avoids querying the system clock
137 * in situations where we know the Linux inode timestamps have
138 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100139 */
140void
141xfs_ichgtime_fast(
142 xfs_inode_t *ip,
143 struct inode *inode,
144 int flags)
145{
146 timespec_t *tvp;
147
Nathan Scott4aeb6642005-11-02 11:43:58 +1100148 if (flags & XFS_ICHGTIME_MOD) {
149 tvp = &inode->i_mtime;
150 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
151 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
152 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100153 if (flags & XFS_ICHGTIME_CHG) {
154 tvp = &inode->i_ctime;
155 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
156 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
157 }
158
159 /*
160 * We update the i_update_core field _after_ changing
161 * the timestamps in order to coordinate properly with
162 * xfs_iflush() so that we don't lose timestamp updates.
163 * This keeps us from having to hold the inode lock
164 * while doing this. We use the SYNCHRONIZE macro to
165 * ensure that the compiler does not reorder the update
166 * of i_update_core above the timestamp updates above.
167 */
168 SYNCHRONIZE();
169 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100170 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100171 mark_inode_dirty_sync(inode);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Nathan Scott446ada42006-01-11 15:35:44 +1100175 * Hook in SELinux. This is not quite correct yet, what we really need
176 * here (as we do for default ACLs) is a mechanism by which creation of
177 * these attrs can be journalled at inode creation time (along with the
178 * inode, of course, such that log replay can't cause these to be lost).
179 */
180STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100181xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +1100182 struct inode *inode,
Nathan Scott446ada42006-01-11 15:35:44 +1100183 struct inode *dir)
184{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100185 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100186 size_t length;
187 void *value;
188 char *name;
189 int error;
190
Christoph Hellwigaf048192008-03-06 13:46:43 +1100191 error = security_inode_init_security(inode, dir, &name,
192 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100193 if (error) {
194 if (error == -EOPNOTSUPP)
195 return 0;
196 return -error;
197 }
198
Christoph Hellwigaf048192008-03-06 13:46:43 +1100199 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100200 if (!error)
Christoph Hellwigaf048192008-03-06 13:46:43 +1100201 xfs_iflags_set(ip, XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100202
203 kfree(name);
204 kfree(value);
205 return error;
206}
207
Barry Naujok556b8b12008-04-10 12:22:07 +1000208static void
209xfs_dentry_to_name(
210 struct xfs_name *namep,
211 struct dentry *dentry)
212{
213 namep->name = dentry->d_name.name;
214 namep->len = dentry->d_name.len;
215}
216
David Chinner7989cb82007-02-10 18:34:56 +1100217STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100218xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000219 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100220 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000221 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100222{
Barry Naujok556b8b12008-04-10 12:22:07 +1000223 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100224
225 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100226 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100227 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100228 * ENOSPC can hit here, among other things.
229 */
Barry Naujok556b8b12008-04-10 12:22:07 +1000230 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100231
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000232 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100233 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100237xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct inode *dir,
239 struct dentry *dentry,
240 int mode,
241 dev_t rdev)
242{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100243 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100244 struct xfs_inode *ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 xfs_acl_t *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000246 struct xfs_name name;
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000247 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 int error;
249
250 /*
251 * Irix uses Missed'em'V split, but doesn't want to see
252 * the upper 5 bits of (14bit) major.
253 */
Nathan Scott220b5282006-03-14 13:33:36 +1100254 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
256
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100257 if (test_default_acl && test_default_acl(dir)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100258 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100260 }
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100261 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 _ACL_FREE(default_acl);
263 default_acl = NULL;
264 }
265 }
266
Barry Naujok556b8b12008-04-10 12:22:07 +1000267 xfs_dentry_to_name(&name, dentry);
268
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100269 if (IS_POSIXACL(dir) && !default_acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 mode &= ~current->fs->umask;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 switch (mode & S_IFMT) {
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100273 case S_IFCHR:
274 case S_IFBLK:
275 case S_IFIFO:
276 case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000277 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 case S_IFREG:
Barry Naujok556b8b12008-04-10 12:22:07 +1000279 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 case S_IFDIR:
Barry Naujok556b8b12008-04-10 12:22:07 +1000282 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284 default:
285 error = EINVAL;
286 break;
287 }
288
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100289 if (unlikely(error))
290 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100291
David Chinner01651642008-08-13 15:45:15 +1000292 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100293
294 error = xfs_init_security(inode, dir);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100295 if (unlikely(error))
296 goto out_cleanup_inode;
297
298 if (default_acl) {
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100299 error = _ACL_INHERIT(inode, mode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100300 if (unlikely(error))
301 goto out_cleanup_inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100302 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 _ACL_FREE(default_acl);
304 }
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100307 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100308 return -error;
309
310 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000311 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100312 out_free_acl:
313 if (default_acl)
314 _ACL_FREE(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -error;
316}
317
318STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100319xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct inode *dir,
321 struct dentry *dentry,
322 int mode,
323 struct nameidata *nd)
324{
Nathan Scott416c6d52006-03-14 14:00:51 +1100325 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100329xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct inode *dir,
331 struct dentry *dentry,
332 int mode)
333{
Nathan Scott416c6d52006-03-14 14:00:51 +1100334 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100338xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct inode *dir,
340 struct dentry *dentry,
341 struct nameidata *nd)
342{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100343 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000344 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int error;
346
347 if (dentry->d_name.len >= MAXNAMELEN)
348 return ERR_PTR(-ENAMETOOLONG);
349
Barry Naujok556b8b12008-04-10 12:22:07 +1000350 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000351 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000352 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (unlikely(error != ENOENT))
354 return ERR_PTR(-error);
355 d_add(dentry, NULL);
356 return NULL;
357 }
358
David Chinner01651642008-08-13 15:45:15 +1000359 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Barry Naujok384f3ce2008-05-21 16:58:22 +1000362STATIC struct dentry *
363xfs_vn_ci_lookup(
364 struct inode *dir,
365 struct dentry *dentry,
366 struct nameidata *nd)
367{
368 struct xfs_inode *ip;
369 struct xfs_name xname;
370 struct xfs_name ci_name;
371 struct qstr dname;
372 int error;
373
374 if (dentry->d_name.len >= MAXNAMELEN)
375 return ERR_PTR(-ENAMETOOLONG);
376
377 xfs_dentry_to_name(&xname, dentry);
378 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
379 if (unlikely(error)) {
380 if (unlikely(error != ENOENT))
381 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000382 /*
383 * call d_add(dentry, NULL) here when d_drop_negative_children
384 * is called in xfs_vn_mknod (ie. allow negative dentries
385 * with CI filesystems).
386 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000387 return NULL;
388 }
389
390 /* if exact match, just splice and exit */
391 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000392 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000393
394 /* else case-insensitive match... */
395 dname.name = ci_name.name;
396 dname.len = ci_name.len;
David Chinner01651642008-08-13 15:45:15 +1000397 dentry = d_add_ci(VFS_I(ip), dentry, &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000398 kmem_free(ci_name.name);
399 return dentry;
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100403xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct dentry *old_dentry,
405 struct inode *dir,
406 struct dentry *dentry)
407{
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100408 struct inode *inode; /* inode of guy being linked to */
Barry Naujok556b8b12008-04-10 12:22:07 +1000409 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int error;
411
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100412 inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000413 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100415 igrab(inode);
Barry Naujok556b8b12008-04-10 12:22:07 +1000416 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Nathan Scott97dfd702006-06-27 16:13:46 +1000417 if (unlikely(error)) {
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100418 iput(inode);
419 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100421
422 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100423 d_instantiate(dentry, inode);
424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100428xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct inode *dir,
430 struct dentry *dentry)
431{
Barry Naujok556b8b12008-04-10 12:22:07 +1000432 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int error;
434
Barry Naujok556b8b12008-04-10 12:22:07 +1000435 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Christoph Hellwige5700702008-06-23 13:25:25 +1000437 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
438 if (error)
439 return error;
440
441 /*
442 * With unlink, the VFS makes the dentry "negative": no inode,
443 * but still hashed. This is incompatible with case-insensitive
444 * mode, so invalidate (unhash) the dentry in CI-mode.
445 */
446 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
447 d_invalidate(dentry);
448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100452xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct inode *dir,
454 struct dentry *dentry,
455 const char *symname)
456{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100457 struct inode *inode;
458 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000459 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000461 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000463 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000464 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Barry Naujok556b8b12008-04-10 12:22:07 +1000465 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Barry Naujok556b8b12008-04-10 12:22:07 +1000467 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100468 if (unlikely(error))
469 goto out;
470
David Chinner01651642008-08-13 15:45:15 +1000471 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100472
473 error = xfs_init_security(inode, dir);
474 if (unlikely(error))
475 goto out_cleanup_inode;
476
477 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100478 return 0;
479
480 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000481 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100482 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -error;
484}
485
486STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100487xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct inode *odir,
489 struct dentry *odentry,
490 struct inode *ndir,
491 struct dentry *ndentry)
492{
493 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000494 struct xfs_name oname;
495 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Barry Naujok556b8b12008-04-10 12:22:07 +1000497 xfs_dentry_to_name(&oname, odentry);
498 xfs_dentry_to_name(&nname, ndentry);
499
Christoph Hellwige5700702008-06-23 13:25:25 +1000500 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000501 XFS_I(ndir), &nname, new_inode ?
502 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
506 * careful here - this function can get called recursively, so
507 * we need to be very careful about how much stack we use.
508 * uio is kmalloced for this reason...
509 */
Al Viro008b1502005-08-20 00:17:39 +0100510STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100511xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct dentry *dentry,
513 struct nameidata *nd)
514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000516 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700518 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000519 if (!link)
520 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000522 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000523 if (unlikely(error))
524 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100527 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000528
529 out_kfree:
530 kfree(link);
531 out_err:
532 nd_set_link(nd, ERR_PTR(error));
533 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Nathan Scottcde410a2005-09-05 11:47:01 +1000536STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100537xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000538 struct dentry *dentry,
539 struct nameidata *nd,
540 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Nathan Scottcde410a2005-09-05 11:47:01 +1000542 char *s = nd_get_link(nd);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (!IS_ERR(s))
545 kfree(s);
546}
547
548#ifdef CONFIG_XFS_POSIX_ACL
549STATIC int
Christoph Hellwig45767582008-02-05 12:13:24 +1100550xfs_check_acl(
551 struct inode *inode,
552 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Christoph Hellwig45767582008-02-05 12:13:24 +1100554 struct xfs_inode *ip = XFS_I(inode);
555 int error;
556
557 xfs_itrace_entry(ip);
558
559 if (XFS_IFORK_Q(ip)) {
560 error = xfs_acl_iaccess(ip, mask, NULL);
561 if (error != -1)
562 return -error;
563 }
564
565 return -EAGAIN;
566}
567
568STATIC int
569xfs_vn_permission(
570 struct inode *inode,
Al Viroe6305c42008-07-15 21:03:57 -0400571 int mask)
Christoph Hellwig45767582008-02-05 12:13:24 +1100572{
573 return generic_permission(inode, mask, xfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100576#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578
579STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100580xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000581 struct vfsmount *mnt,
582 struct dentry *dentry,
583 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000585 struct inode *inode = dentry->d_inode;
586 struct xfs_inode *ip = XFS_I(inode);
587 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000589 xfs_itrace_entry(ip);
590
591 if (XFS_FORCED_SHUTDOWN(mp))
592 return XFS_ERROR(EIO);
593
594 stat->size = XFS_ISIZE(ip);
595 stat->dev = inode->i_sb->s_dev;
596 stat->mode = ip->i_d.di_mode;
597 stat->nlink = ip->i_d.di_nlink;
598 stat->uid = ip->i_d.di_uid;
599 stat->gid = ip->i_d.di_gid;
600 stat->ino = ip->i_ino;
601#if XFS_BIG_INUMS
602 stat->ino += mp->m_inoadd;
603#endif
604 stat->atime = inode->i_atime;
605 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
606 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
607 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
608 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
609 stat->blocks =
610 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
611
612
613 switch (inode->i_mode & S_IFMT) {
614 case S_IFBLK:
615 case S_IFCHR:
616 stat->blksize = BLKDEV_IOSIZE;
617 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
618 sysv_minor(ip->i_df.if_u2.if_rdev));
619 break;
620 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100621 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000622 /*
623 * If the file blocks are being allocated from a
624 * realtime volume, then return the inode's realtime
625 * extent size or the realtime volume's extent size.
626 */
627 stat->blksize =
628 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
629 } else
630 stat->blksize = xfs_preferred_iosize(mp);
631 stat->rdev = 0;
632 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000633 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000634
635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100639xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct dentry *dentry,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000641 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000643 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
David Chinnerd87dd632008-04-10 12:21:46 +1000646/*
647 * block_truncate_page can return an error, but we can't propagate it
648 * at all here. Leave a complaint + stack trace in the syslog because
649 * this could be bad. If it is bad, we need to propagate the error further.
650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100652xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct inode *inode)
654{
David Chinnerd87dd632008-04-10 12:21:46 +1000655 int error;
656 error = block_truncate_page(inode->i_mapping, inode->i_size,
657 xfs_get_blocks);
658 WARN_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
David Chinner3ed65262007-11-23 16:29:25 +1100661STATIC long
662xfs_vn_fallocate(
663 struct inode *inode,
664 int mode,
665 loff_t offset,
666 loff_t len)
667{
668 long error;
669 loff_t new_size = 0;
670 xfs_flock64_t bf;
671 xfs_inode_t *ip = XFS_I(inode);
672
673 /* preallocation on directories not yet supported */
674 error = -ENODEV;
675 if (S_ISDIR(inode->i_mode))
676 goto out_error;
677
678 bf.l_whence = 0;
679 bf.l_start = offset;
680 bf.l_len = len;
681
682 xfs_ilock(ip, XFS_IOLOCK_EXCL);
683 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000684 0, NULL, XFS_ATTR_NOLOCK);
David Chinner3ed65262007-11-23 16:29:25 +1100685 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
686 offset + len > i_size_read(inode))
687 new_size = offset + len;
688
689 /* Change file size if needed */
690 if (new_size) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000691 struct iattr iattr;
David Chinner3ed65262007-11-23 16:29:25 +1100692
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000693 iattr.ia_valid = ATTR_SIZE;
694 iattr.ia_size = new_size;
695 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
David Chinner3ed65262007-11-23 16:29:25 +1100696 }
697
698 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
699out_error:
700 return error;
701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000703static const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100704 .permission = xfs_vn_permission,
705 .truncate = xfs_vn_truncate,
706 .getattr = xfs_vn_getattr,
707 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000708 .setxattr = generic_setxattr,
709 .getxattr = generic_getxattr,
710 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100711 .listxattr = xfs_vn_listxattr,
David Chinner3ed65262007-11-23 16:29:25 +1100712 .fallocate = xfs_vn_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000715static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100716 .create = xfs_vn_create,
717 .lookup = xfs_vn_lookup,
718 .link = xfs_vn_link,
719 .unlink = xfs_vn_unlink,
720 .symlink = xfs_vn_symlink,
721 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000722 /*
723 * Yes, XFS uses the same method for rmdir and unlink.
724 *
725 * There are some subtile differences deeper in the code,
726 * but we use S_ISDIR to check for those.
727 */
728 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100729 .mknod = xfs_vn_mknod,
730 .rename = xfs_vn_rename,
731 .permission = xfs_vn_permission,
732 .getattr = xfs_vn_getattr,
733 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000734 .setxattr = generic_setxattr,
735 .getxattr = generic_getxattr,
736 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100737 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738};
739
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000740static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +1000741 .create = xfs_vn_create,
742 .lookup = xfs_vn_ci_lookup,
743 .link = xfs_vn_link,
744 .unlink = xfs_vn_unlink,
745 .symlink = xfs_vn_symlink,
746 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000747 /*
748 * Yes, XFS uses the same method for rmdir and unlink.
749 *
750 * There are some subtile differences deeper in the code,
751 * but we use S_ISDIR to check for those.
752 */
753 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000754 .mknod = xfs_vn_mknod,
755 .rename = xfs_vn_rename,
756 .permission = xfs_vn_permission,
757 .getattr = xfs_vn_getattr,
758 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000759 .setxattr = generic_setxattr,
760 .getxattr = generic_getxattr,
761 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000762 .listxattr = xfs_vn_listxattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000763};
764
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000765static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100767 .follow_link = xfs_vn_follow_link,
768 .put_link = xfs_vn_put_link,
769 .permission = xfs_vn_permission,
770 .getattr = xfs_vn_getattr,
771 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +1000772 .setxattr = generic_setxattr,
773 .getxattr = generic_getxattr,
774 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +1100775 .listxattr = xfs_vn_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776};
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000777
778STATIC void
779xfs_diflags_to_iflags(
780 struct inode *inode,
781 struct xfs_inode *ip)
782{
783 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
784 inode->i_flags |= S_IMMUTABLE;
785 else
786 inode->i_flags &= ~S_IMMUTABLE;
787 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
788 inode->i_flags |= S_APPEND;
789 else
790 inode->i_flags &= ~S_APPEND;
791 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
792 inode->i_flags |= S_SYNC;
793 else
794 inode->i_flags &= ~S_SYNC;
795 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
796 inode->i_flags |= S_NOATIME;
797 else
798 inode->i_flags &= ~S_NOATIME;
799}
800
801/*
802 * Initialize the Linux inode, set up the operation vectors and
803 * unlock the inode.
804 *
805 * When reading existing inodes from disk this is called directly
806 * from xfs_iget, when creating a new inode it is called from
807 * xfs_ialloc after setting up the inode.
808 */
809void
810xfs_setup_inode(
811 struct xfs_inode *ip)
812{
813 struct inode *inode = ip->i_vnode;
814
815 inode->i_mode = ip->i_d.di_mode;
816 inode->i_nlink = ip->i_d.di_nlink;
817 inode->i_uid = ip->i_d.di_uid;
818 inode->i_gid = ip->i_d.di_gid;
819
820 switch (inode->i_mode & S_IFMT) {
821 case S_IFBLK:
822 case S_IFCHR:
823 inode->i_rdev =
824 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
825 sysv_minor(ip->i_df.if_u2.if_rdev));
826 break;
827 default:
828 inode->i_rdev = 0;
829 break;
830 }
831
832 inode->i_generation = ip->i_d.di_gen;
833 i_size_write(inode, ip->i_d.di_size);
834 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
835 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
836 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
837 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
838 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
839 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
840 xfs_diflags_to_iflags(inode, ip);
841 xfs_iflags_clear(ip, XFS_IMODIFIED);
842
843 switch (inode->i_mode & S_IFMT) {
844 case S_IFREG:
845 inode->i_op = &xfs_inode_operations;
846 inode->i_fop = &xfs_file_operations;
847 inode->i_mapping->a_ops = &xfs_address_space_operations;
848 break;
849 case S_IFDIR:
850 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
851 inode->i_op = &xfs_dir_ci_inode_operations;
852 else
853 inode->i_op = &xfs_dir_inode_operations;
854 inode->i_fop = &xfs_dir_file_operations;
855 break;
856 case S_IFLNK:
857 inode->i_op = &xfs_symlink_inode_operations;
858 if (!(ip->i_df.if_flags & XFS_IFINLINE))
859 inode->i_mapping->a_ops = &xfs_address_space_operations;
860 break;
861 default:
862 inode->i_op = &xfs_inode_operations;
863 init_special_inode(inode, inode->i_mode, inode->i_rdev);
864 break;
865 }
866
867 xfs_iflags_clear(ip, XFS_INEW);
868 barrier();
869
870 unlock_new_inode(inode);
871}