blob: b5afcfcdc7d5aca11bffb14bcbd1aaf81b73a1a0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Nathan Scott4aeb6642005-11-02 11:43:58 +110056/*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110057 * Bring the atime in the XFS inode uptodate.
58 * Used before logging the inode to disk or when the Linux inode goes away.
59 */
60void
61xfs_synchronize_atime(
62 xfs_inode_t *ip)
63{
Nathan Scott67fcaa72006-06-09 17:00:52 +100064 bhv_vnode_t *vp;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110065
66 vp = XFS_ITOV_NULL(ip);
67 if (vp) {
Christoph Hellwig0a74cd12007-08-29 11:53:12 +100068 ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
69 ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110070 }
71}
72
73/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110074 * Change the requested timestamp in the given inode.
75 * We don't lock across timestamp updates, and we don't log them but
76 * we do record the fact that there is dirty information in core.
77 *
78 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
79 * with XFS_ICHGTIME_ACC to be sure that access time
80 * update will take. Calling first with XFS_ICHGTIME_ACC
81 * and then XFS_ICHGTIME_MOD may fail to modify the access
82 * timestamp if the filesystem is mounted noacctm.
83 */
84void
85xfs_ichgtime(
86 xfs_inode_t *ip,
87 int flags)
88{
Nathan Scottec86dc02006-03-17 17:25:36 +110089 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +110090 timespec_t tv;
91
Nathan Scott4aeb6642005-11-02 11:43:58 +110092 nanotime(&tv);
93 if (flags & XFS_ICHGTIME_MOD) {
94 inode->i_mtime = tv;
95 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
96 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
97 }
98 if (flags & XFS_ICHGTIME_ACC) {
99 inode->i_atime = tv;
100 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
101 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
102 }
103 if (flags & XFS_ICHGTIME_CHG) {
104 inode->i_ctime = tv;
105 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
106 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
107 }
108
109 /*
110 * We update the i_update_core field _after_ changing
111 * the timestamps in order to coordinate properly with
112 * xfs_iflush() so that we don't lose timestamp updates.
113 * This keeps us from having to hold the inode lock
114 * while doing this. We use the SYNCHRONIZE macro to
115 * ensure that the compiler does not reorder the update
116 * of i_update_core above the timestamp updates above.
117 */
118 SYNCHRONIZE();
119 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100120 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100121 mark_inode_dirty_sync(inode);
122}
123
124/*
125 * Variant on the above which avoids querying the system clock
126 * in situations where we know the Linux inode timestamps have
127 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100128 */
129void
130xfs_ichgtime_fast(
131 xfs_inode_t *ip,
132 struct inode *inode,
133 int flags)
134{
135 timespec_t *tvp;
136
137 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100138 * Atime updates for read() & friends are handled lazily now, and
139 * explicit updates must go through xfs_ichgtime()
140 */
141 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
142
143 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100144 * We're not supposed to change timestamps in readonly-mounted
145 * filesystems. Throw it away if anyone asks us.
146 */
147 if (unlikely(IS_RDONLY(inode)))
148 return;
149
Nathan Scott4aeb6642005-11-02 11:43:58 +1100150 if (flags & XFS_ICHGTIME_MOD) {
151 tvp = &inode->i_mtime;
152 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
153 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
154 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100155 if (flags & XFS_ICHGTIME_CHG) {
156 tvp = &inode->i_ctime;
157 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
158 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
159 }
160
161 /*
162 * We update the i_update_core field _after_ changing
163 * the timestamps in order to coordinate properly with
164 * xfs_iflush() so that we don't lose timestamp updates.
165 * This keeps us from having to hold the inode lock
166 * while doing this. We use the SYNCHRONIZE macro to
167 * ensure that the compiler does not reorder the update
168 * of i_update_core above the timestamp updates above.
169 */
170 SYNCHRONIZE();
171 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100172 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100173 mark_inode_dirty_sync(inode);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/*
178 * Pull the link count and size up from the xfs inode to the linux inode
179 */
180STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100181xfs_validate_fields(
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000182 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000184 struct xfs_inode *ip = XFS_I(inode);
185 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000187 inode->i_nlink = ip->i_d.di_nlink;
188 inode->i_blocks =
189 XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks +
190 ip->i_delayed_blks);
191 /* we're under i_sem so i_size can't change under us */
192 size = XFS_ISIZE(ip);
193 if (i_size_read(inode) != size)
194 i_size_write(inode, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
Nathan Scott446ada42006-01-11 15:35:44 +1100198 * Hook in SELinux. This is not quite correct yet, what we really need
199 * here (as we do for default ACLs) is a mechanism by which creation of
200 * these attrs can be journalled at inode creation time (along with the
201 * inode, of course, such that log replay can't cause these to be lost).
202 */
203STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100204xfs_init_security(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000205 bhv_vnode_t *vp,
Nathan Scott446ada42006-01-11 15:35:44 +1100206 struct inode *dir)
207{
Nathan Scottec86dc02006-03-17 17:25:36 +1100208 struct inode *ip = vn_to_inode(vp);
Nathan Scott446ada42006-01-11 15:35:44 +1100209 size_t length;
210 void *value;
211 char *name;
212 int error;
213
214 error = security_inode_init_security(ip, dir, &name, &value, &length);
215 if (error) {
216 if (error == -EOPNOTSUPP)
217 return 0;
218 return -error;
219 }
220
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000221 error = xfs_attr_set(XFS_I(ip), name, value,
222 length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100223 if (!error)
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000224 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100225
226 kfree(name);
227 kfree(value);
228 return error;
229}
230
231/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * Determine whether a process has a valid fs_struct (kernel daemons
233 * like knfsd don't have an fs_struct).
234 *
235 * XXX(hch): nfsd is broken, better fix it instead.
236 */
David Chinner7989cb82007-02-10 18:34:56 +1100237STATIC_INLINE int
Nathan Scott416c6d52006-03-14 14:00:51 +1100238xfs_has_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 return (task->fs != init_task.fs);
241}
242
David Chinner7989cb82007-02-10 18:34:56 +1100243STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100244xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000245 struct inode *dir,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000246 bhv_vnode_t *vp,
Nathan Scott220b5282006-03-14 13:33:36 +1100247 struct dentry *dentry,
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100248 int mode)
249{
250 struct dentry teardown = {};
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100251
252 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100253 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100254 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100255 * ENOSPC can hit here, among other things.
256 */
Nathan Scottec86dc02006-03-17 17:25:36 +1100257 teardown.d_inode = vn_to_inode(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100258 teardown.d_name = dentry->d_name;
259
260 if (S_ISDIR(mode))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000261 xfs_rmdir(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100262 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000263 xfs_remove(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100264 VN_RELE(vp);
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100268xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct inode *dir,
270 struct dentry *dentry,
271 int mode,
272 dev_t rdev)
273{
274 struct inode *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000275 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 xfs_acl_t *default_acl = NULL;
277 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
278 int error;
279
280 /*
281 * Irix uses Missed'em'V split, but doesn't want to see
282 * the upper 5 bits of (14bit) major.
283 */
Nathan Scott220b5282006-03-14 13:33:36 +1100284 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -EINVAL;
286
Nathan Scott220b5282006-03-14 13:33:36 +1100287 if (unlikely(test_default_acl && test_default_acl(dvp))) {
288 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
292 _ACL_FREE(default_acl);
293 default_acl = NULL;
294 }
295 }
296
Nathan Scott416c6d52006-03-14 14:00:51 +1100297 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 mode &= ~current->fs->umask;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 switch (mode & S_IFMT) {
301 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000302 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 case S_IFREG:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000304 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
306 case S_IFDIR:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000307 error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309 default:
310 error = EINVAL;
311 break;
312 }
313
Nathan Scott220b5282006-03-14 13:33:36 +1100314 if (unlikely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100315 error = xfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100316 if (error)
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000317 xfs_cleanup_inode(dir, vp, dentry, mode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100318 }
Nathan Scott446ada42006-01-11 15:35:44 +1100319
Nathan Scott220b5282006-03-14 13:33:36 +1100320 if (unlikely(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!error) {
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000322 error = _ACL_INHERIT(vp, mode, default_acl);
Nathan Scott220b5282006-03-14 13:33:36 +1100323 if (!error)
Christoph Hellwig0a74cd12007-08-29 11:53:12 +1000324 xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100325 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000326 xfs_cleanup_inode(dir, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 _ACL_FREE(default_acl);
329 }
330
Nathan Scott220b5282006-03-14 13:33:36 +1100331 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ASSERT(vp);
Nathan Scottec86dc02006-03-17 17:25:36 +1100333 ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Christoph Hellwigbad60fd2007-12-21 10:58:56 +1100335 if (S_ISDIR(mode))
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000336 xfs_validate_fields(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 d_instantiate(dentry, ip);
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000338 xfs_validate_fields(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 return -error;
341}
342
343STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100344xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct inode *dir,
346 struct dentry *dentry,
347 int mode,
348 struct nameidata *nd)
349{
Nathan Scott416c6d52006-03-14 14:00:51 +1100350 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100354xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct inode *dir,
356 struct dentry *dentry,
357 int mode)
358{
Nathan Scott416c6d52006-03-14 14:00:51 +1100359 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100363xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct inode *dir,
365 struct dentry *dentry,
366 struct nameidata *nd)
367{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000368 bhv_vnode_t *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int error;
370
371 if (dentry->d_name.len >= MAXNAMELEN)
372 return ERR_PTR(-ENAMETOOLONG);
373
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000374 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000375 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (unlikely(error != ENOENT))
377 return ERR_PTR(-error);
378 d_add(dentry, NULL);
379 return NULL;
380 }
381
Nathan Scottec86dc02006-03-17 17:25:36 +1100382 return d_splice_alias(vn_to_inode(cvp), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100386xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct dentry *old_dentry,
388 struct inode *dir,
389 struct dentry *dentry)
390{
391 struct inode *ip; /* inode of guy being linked to */
Nathan Scott67fcaa72006-06-09 17:00:52 +1000392 bhv_vnode_t *vp; /* vp of name being linked */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int error;
394
395 ip = old_dentry->d_inode; /* inode being linked to */
Nathan Scottec86dc02006-03-17 17:25:36 +1100396 vp = vn_from_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Nathan Scott97dfd702006-06-27 16:13:46 +1000398 VN_HOLD(vp);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000399 error = xfs_link(XFS_I(dir), vp, dentry);
Nathan Scott97dfd702006-06-27 16:13:46 +1000400 if (unlikely(error)) {
401 VN_RELE(vp);
402 } else {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000403 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000404 xfs_validate_fields(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 d_instantiate(dentry, ip);
406 }
407 return -error;
408}
409
410STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100411xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct inode *dir,
413 struct dentry *dentry)
414{
415 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int error;
417
418 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000420 error = xfs_remove(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100421 if (likely(!error)) {
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000422 xfs_validate_fields(dir); /* size needs update */
423 xfs_validate_fields(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -error;
426}
427
428STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100429xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct inode *dir,
431 struct dentry *dentry,
432 const char *symname)
433{
434 struct inode *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000435 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000437 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 cvp = NULL;
440
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000441 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000442 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000444 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
445 &cvp, NULL);
Nathan Scott54245702006-01-16 15:54:05 +1100446 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100447 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100448 if (likely(!error)) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100449 ip = vn_to_inode(cvp);
Nathan Scott54245702006-01-16 15:54:05 +1100450 d_instantiate(dentry, ip);
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000451 xfs_validate_fields(dir);
452 xfs_validate_fields(ip);
Nathan Scottce9d37c2006-03-17 17:26:34 +1100453 } else {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000454 xfs_cleanup_inode(dir, cvp, dentry, 0);
Nathan Scott54245702006-01-16 15:54:05 +1100455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 return -error;
458}
459
460STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100461xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct inode *dir,
463 struct dentry *dentry)
464{
465 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int error;
467
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000468 error = xfs_rmdir(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100469 if (likely(!error)) {
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000470 xfs_validate_fields(inode);
471 xfs_validate_fields(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 return -error;
474}
475
476STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100477xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct inode *odir,
479 struct dentry *odentry,
480 struct inode *ndir,
481 struct dentry *ndentry)
482{
483 struct inode *new_inode = ndentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000484 bhv_vnode_t *tvp; /* target directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int error;
486
Nathan Scottec86dc02006-03-17 17:25:36 +1100487 tvp = vn_from_inode(ndir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000489 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100490 if (likely(!error)) {
491 if (new_inode)
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000492 xfs_validate_fields(new_inode);
493 xfs_validate_fields(odir);
Nathan Scott220b5282006-03-14 13:33:36 +1100494 if (ndir != odir)
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000495 xfs_validate_fields(ndir);
Nathan Scott220b5282006-03-14 13:33:36 +1100496 }
Nathan Scott220b5282006-03-14 13:33:36 +1100497 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/*
501 * careful here - this function can get called recursively, so
502 * we need to be very careful about how much stack we use.
503 * uio is kmalloced for this reason...
504 */
Al Viro008b1502005-08-20 00:17:39 +0100505STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100506xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct dentry *dentry,
508 struct nameidata *nd)
509{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000511 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700513 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000514 if (!link)
515 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000517 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000518 if (unlikely(error))
519 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100522 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000523
524 out_kfree:
525 kfree(link);
526 out_err:
527 nd_set_link(nd, ERR_PTR(error));
528 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Nathan Scottcde410a2005-09-05 11:47:01 +1000531STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100532xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000533 struct dentry *dentry,
534 struct nameidata *nd,
535 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Nathan Scottcde410a2005-09-05 11:47:01 +1000537 char *s = nd_get_link(nd);
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!IS_ERR(s))
540 kfree(s);
541}
542
543#ifdef CONFIG_XFS_POSIX_ACL
544STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100545xfs_vn_permission(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct inode *inode,
547 int mode,
548 struct nameidata *nd)
549{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000550 return -xfs_access(XFS_I(inode), mode << 6, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100553#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555
556STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100557xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000558 struct vfsmount *mnt,
559 struct dentry *dentry,
560 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000562 struct inode *inode = dentry->d_inode;
563 struct xfs_inode *ip = XFS_I(inode);
564 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000566 xfs_itrace_entry(ip);
567
568 if (XFS_FORCED_SHUTDOWN(mp))
569 return XFS_ERROR(EIO);
570
571 stat->size = XFS_ISIZE(ip);
572 stat->dev = inode->i_sb->s_dev;
573 stat->mode = ip->i_d.di_mode;
574 stat->nlink = ip->i_d.di_nlink;
575 stat->uid = ip->i_d.di_uid;
576 stat->gid = ip->i_d.di_gid;
577 stat->ino = ip->i_ino;
578#if XFS_BIG_INUMS
579 stat->ino += mp->m_inoadd;
580#endif
581 stat->atime = inode->i_atime;
582 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
583 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
584 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
585 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
586 stat->blocks =
587 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
588
589
590 switch (inode->i_mode & S_IFMT) {
591 case S_IFBLK:
592 case S_IFCHR:
593 stat->blksize = BLKDEV_IOSIZE;
594 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
595 sysv_minor(ip->i_df.if_u2.if_rdev));
596 break;
597 default:
598 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
599 /*
600 * If the file blocks are being allocated from a
601 * realtime volume, then return the inode's realtime
602 * extent size or the realtime volume's extent size.
603 */
604 stat->blksize =
605 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
606 } else
607 stat->blksize = xfs_preferred_iosize(mp);
608 stat->rdev = 0;
609 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000610 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000611
612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100616xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct dentry *dentry,
618 struct iattr *attr)
619{
620 struct inode *inode = dentry->d_inode;
621 unsigned int ia_valid = attr->ia_valid;
Nathan Scott8285fb52006-06-09 17:07:12 +1000622 bhv_vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int flags = 0;
624 int error;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (ia_valid & ATTR_UID) {
627 vattr.va_mask |= XFS_AT_UID;
628 vattr.va_uid = attr->ia_uid;
629 }
630 if (ia_valid & ATTR_GID) {
631 vattr.va_mask |= XFS_AT_GID;
632 vattr.va_gid = attr->ia_gid;
633 }
634 if (ia_valid & ATTR_SIZE) {
635 vattr.va_mask |= XFS_AT_SIZE;
636 vattr.va_size = attr->ia_size;
637 }
638 if (ia_valid & ATTR_ATIME) {
639 vattr.va_mask |= XFS_AT_ATIME;
640 vattr.va_atime = attr->ia_atime;
Nathan Scott8c0b5112006-04-11 15:12:45 +1000641 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 if (ia_valid & ATTR_MTIME) {
644 vattr.va_mask |= XFS_AT_MTIME;
645 vattr.va_mtime = attr->ia_mtime;
646 }
647 if (ia_valid & ATTR_CTIME) {
648 vattr.va_mask |= XFS_AT_CTIME;
649 vattr.va_ctime = attr->ia_ctime;
650 }
651 if (ia_valid & ATTR_MODE) {
652 vattr.va_mask |= XFS_AT_MODE;
653 vattr.va_mode = attr->ia_mode;
654 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
655 inode->i_mode &= ~S_ISGID;
656 }
657
658 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
659 flags |= ATTR_UTIME;
660#ifdef ATTR_NO_BLOCK
661 if ((ia_valid & ATTR_NO_BLOCK))
662 flags |= ATTR_NONBLOCK;
663#endif
664
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000665 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100666 if (likely(!error))
Christoph Hellwig21a62542007-09-19 15:27:49 +1000667 vn_revalidate(vn_from_inode(inode));
Nathan Scott220b5282006-03-14 13:33:36 +1100668 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100672xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct inode *inode)
674{
Nathan Scottc2536662006-03-29 10:44:40 +1000675 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100679xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct dentry *dentry,
681 const char *name,
682 const void *data,
683 size_t size,
684 int flags)
685{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000686 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 char *attr = (char *)name;
688 attrnames_t *namesp;
689 int xflags = 0;
690 int error;
691
692 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
693 if (!namesp)
694 return -EOPNOTSUPP;
695 attr += namesp->attr_namelen;
696 error = namesp->attr_capable(vp, NULL);
697 if (error)
698 return error;
699
700 /* Convert Linux syscall to XFS internal ATTR flags */
701 if (flags & XATTR_CREATE)
702 xflags |= ATTR_CREATE;
703 if (flags & XATTR_REPLACE)
704 xflags |= ATTR_REPLACE;
705 xflags |= namesp->attr_flag;
706 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
707}
708
709STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100710xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct dentry *dentry,
712 const char *name,
713 void *data,
714 size_t size)
715{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000716 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 char *attr = (char *)name;
718 attrnames_t *namesp;
719 int xflags = 0;
720 ssize_t error;
721
722 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
723 if (!namesp)
724 return -EOPNOTSUPP;
725 attr += namesp->attr_namelen;
726 error = namesp->attr_capable(vp, NULL);
727 if (error)
728 return error;
729
730 /* Convert Linux syscall to XFS internal ATTR flags */
731 if (!size) {
732 xflags |= ATTR_KERNOVAL;
733 data = NULL;
734 }
735 xflags |= namesp->attr_flag;
736 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
737}
738
739STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100740xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct dentry *dentry,
742 char *data,
743 size_t size)
744{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000745 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int error, xflags = ATTR_KERNAMELS;
747 ssize_t result;
748
749 if (!size)
750 xflags |= ATTR_KERNOVAL;
751 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
752
753 error = attr_generic_list(vp, data, size, xflags, &result);
754 if (error < 0)
755 return error;
756 return result;
757}
758
759STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100760xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct dentry *dentry,
762 const char *name)
763{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000764 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 char *attr = (char *)name;
766 attrnames_t *namesp;
767 int xflags = 0;
768 int error;
769
770 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
771 if (!namesp)
772 return -EOPNOTSUPP;
773 attr += namesp->attr_namelen;
774 error = namesp->attr_capable(vp, NULL);
775 if (error)
776 return error;
777 xflags |= namesp->attr_flag;
778 return namesp->attr_remove(vp, attr, xflags);
779}
780
781
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800782const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100783 .permission = xfs_vn_permission,
784 .truncate = xfs_vn_truncate,
785 .getattr = xfs_vn_getattr,
786 .setattr = xfs_vn_setattr,
787 .setxattr = xfs_vn_setxattr,
788 .getxattr = xfs_vn_getxattr,
789 .listxattr = xfs_vn_listxattr,
790 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791};
792
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800793const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100794 .create = xfs_vn_create,
795 .lookup = xfs_vn_lookup,
796 .link = xfs_vn_link,
797 .unlink = xfs_vn_unlink,
798 .symlink = xfs_vn_symlink,
799 .mkdir = xfs_vn_mkdir,
800 .rmdir = xfs_vn_rmdir,
801 .mknod = xfs_vn_mknod,
802 .rename = xfs_vn_rename,
803 .permission = xfs_vn_permission,
804 .getattr = xfs_vn_getattr,
805 .setattr = xfs_vn_setattr,
806 .setxattr = xfs_vn_setxattr,
807 .getxattr = xfs_vn_getxattr,
808 .listxattr = xfs_vn_listxattr,
809 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810};
811
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800812const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100814 .follow_link = xfs_vn_follow_link,
815 .put_link = xfs_vn_put_link,
816 .permission = xfs_vn_permission,
817 .getattr = xfs_vn_getattr,
818 .setattr = xfs_vn_setattr,
819 .setxattr = xfs_vn_setxattr,
820 .getxattr = xfs_vn_getxattr,
821 .listxattr = xfs_vn_listxattr,
822 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823};