blob: ef941f99b2bc7ee6cfb639b2c5645d1501e27922 [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"
49
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080050#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/xattr.h>
52#include <linux/namei.h>
Nathan Scott446ada42006-01-11 15:35:44 +110053#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Nathan Scott4aeb6642005-11-02 11:43:58 +110055/*
Christoph Hellwig75e17b32006-01-11 20:58:44 +110056 * Get a XFS inode from a given vnode.
57 */
58xfs_inode_t *
59xfs_vtoi(
Nathan Scott67fcaa72006-06-09 17:00:52 +100060 bhv_vnode_t *vp)
Christoph Hellwig75e17b32006-01-11 20:58:44 +110061{
62 bhv_desc_t *bdp;
63
64 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
65 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
66 if (unlikely(bdp == NULL))
67 return NULL;
68 return XFS_BHVTOI(bdp);
69}
70
71/*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110072 * Bring the atime in the XFS inode uptodate.
73 * Used before logging the inode to disk or when the Linux inode goes away.
74 */
75void
76xfs_synchronize_atime(
77 xfs_inode_t *ip)
78{
Nathan Scott67fcaa72006-06-09 17:00:52 +100079 bhv_vnode_t *vp;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110080
81 vp = XFS_ITOV_NULL(ip);
82 if (vp) {
83 struct inode *inode = &vp->v_inode;
84 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
85 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
86 }
87}
88
89/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110090 * Change the requested timestamp in the given inode.
91 * We don't lock across timestamp updates, and we don't log them but
92 * we do record the fact that there is dirty information in core.
93 *
94 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
95 * with XFS_ICHGTIME_ACC to be sure that access time
96 * update will take. Calling first with XFS_ICHGTIME_ACC
97 * and then XFS_ICHGTIME_MOD may fail to modify the access
98 * timestamp if the filesystem is mounted noacctm.
99 */
100void
101xfs_ichgtime(
102 xfs_inode_t *ip,
103 int flags)
104{
Nathan Scottec86dc02006-03-17 17:25:36 +1100105 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +1100106 timespec_t tv;
107
Nathan Scott4aeb6642005-11-02 11:43:58 +1100108 nanotime(&tv);
109 if (flags & XFS_ICHGTIME_MOD) {
110 inode->i_mtime = tv;
111 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
112 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
113 }
114 if (flags & XFS_ICHGTIME_ACC) {
115 inode->i_atime = tv;
116 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
117 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
118 }
119 if (flags & XFS_ICHGTIME_CHG) {
120 inode->i_ctime = tv;
121 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
122 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
123 }
124
125 /*
126 * We update the i_update_core field _after_ changing
127 * the timestamps in order to coordinate properly with
128 * xfs_iflush() so that we don't lose timestamp updates.
129 * This keeps us from having to hold the inode lock
130 * while doing this. We use the SYNCHRONIZE macro to
131 * ensure that the compiler does not reorder the update
132 * of i_update_core above the timestamp updates above.
133 */
134 SYNCHRONIZE();
135 ip->i_update_core = 1;
136 if (!(inode->i_state & I_LOCK))
137 mark_inode_dirty_sync(inode);
138}
139
140/*
141 * Variant on the above which avoids querying the system clock
142 * in situations where we know the Linux inode timestamps have
143 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100144 */
145void
146xfs_ichgtime_fast(
147 xfs_inode_t *ip,
148 struct inode *inode,
149 int flags)
150{
151 timespec_t *tvp;
152
153 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100154 * Atime updates for read() & friends are handled lazily now, and
155 * explicit updates must go through xfs_ichgtime()
156 */
157 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
158
159 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100160 * We're not supposed to change timestamps in readonly-mounted
161 * filesystems. Throw it away if anyone asks us.
162 */
163 if (unlikely(IS_RDONLY(inode)))
164 return;
165
Nathan Scott4aeb6642005-11-02 11:43:58 +1100166 if (flags & XFS_ICHGTIME_MOD) {
167 tvp = &inode->i_mtime;
168 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
169 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
170 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100171 if (flags & XFS_ICHGTIME_CHG) {
172 tvp = &inode->i_ctime;
173 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
174 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
175 }
176
177 /*
178 * We update the i_update_core field _after_ changing
179 * the timestamps in order to coordinate properly with
180 * xfs_iflush() so that we don't lose timestamp updates.
181 * This keeps us from having to hold the inode lock
182 * while doing this. We use the SYNCHRONIZE macro to
183 * ensure that the compiler does not reorder the update
184 * of i_update_core above the timestamp updates above.
185 */
186 SYNCHRONIZE();
187 ip->i_update_core = 1;
188 if (!(inode->i_state & I_LOCK))
189 mark_inode_dirty_sync(inode);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/*
194 * Pull the link count and size up from the xfs inode to the linux inode
195 */
196STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100197xfs_validate_fields(
Nathan Scott220b5282006-03-14 13:33:36 +1100198 struct inode *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +1000199 bhv_vattr_t *vattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Nathan Scott220b5282006-03-14 13:33:36 +1100201 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000202 if (!bhv_vop_getattr(vn_from_inode(ip), vattr, ATTR_LAZY, NULL)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100203 ip->i_nlink = vattr->va_nlink;
204 ip->i_blocks = vattr->va_nblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Nathan Scott220b5282006-03-14 13:33:36 +1100206 /* we're under i_sem so i_size can't change under us */
207 if (i_size_read(ip) != vattr->va_size)
208 i_size_write(ip, vattr->va_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210}
211
212/*
Nathan Scott446ada42006-01-11 15:35:44 +1100213 * Hook in SELinux. This is not quite correct yet, what we really need
214 * here (as we do for default ACLs) is a mechanism by which creation of
215 * these attrs can be journalled at inode creation time (along with the
216 * inode, of course, such that log replay can't cause these to be lost).
217 */
218STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100219xfs_init_security(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000220 bhv_vnode_t *vp,
Nathan Scott446ada42006-01-11 15:35:44 +1100221 struct inode *dir)
222{
Nathan Scottec86dc02006-03-17 17:25:36 +1100223 struct inode *ip = vn_to_inode(vp);
Nathan Scott446ada42006-01-11 15:35:44 +1100224 size_t length;
225 void *value;
226 char *name;
227 int error;
228
229 error = security_inode_init_security(ip, dir, &name, &value, &length);
230 if (error) {
231 if (error == -EOPNOTSUPP)
232 return 0;
233 return -error;
234 }
235
Nathan Scott67fcaa72006-06-09 17:00:52 +1000236 error = bhv_vop_attr_set(vp, name, value, length, ATTR_SECURE, NULL);
Nathan Scott446ada42006-01-11 15:35:44 +1100237 if (!error)
238 VMODIFY(vp);
239
240 kfree(name);
241 kfree(value);
242 return error;
243}
244
245/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * Determine whether a process has a valid fs_struct (kernel daemons
247 * like knfsd don't have an fs_struct).
248 *
249 * XXX(hch): nfsd is broken, better fix it instead.
250 */
David Chinner7989cb82007-02-10 18:34:56 +1100251STATIC_INLINE int
Nathan Scott416c6d52006-03-14 14:00:51 +1100252xfs_has_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 return (task->fs != init_task.fs);
255}
256
David Chinner7989cb82007-02-10 18:34:56 +1100257STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100258xfs_cleanup_inode(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000259 bhv_vnode_t *dvp,
260 bhv_vnode_t *vp,
Nathan Scott220b5282006-03-14 13:33:36 +1100261 struct dentry *dentry,
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100262 int mode)
263{
264 struct dentry teardown = {};
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100265
266 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100267 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100268 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100269 * ENOSPC can hit here, among other things.
270 */
Nathan Scottec86dc02006-03-17 17:25:36 +1100271 teardown.d_inode = vn_to_inode(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100272 teardown.d_name = dentry->d_name;
273
274 if (S_ISDIR(mode))
Nathan Scott67fcaa72006-06-09 17:00:52 +1000275 bhv_vop_rmdir(dvp, &teardown, NULL);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100276 else
Nathan Scott67fcaa72006-06-09 17:00:52 +1000277 bhv_vop_remove(dvp, &teardown, NULL);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100278 VN_RELE(vp);
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100282xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct inode *dir,
284 struct dentry *dentry,
285 int mode,
286 dev_t rdev)
287{
288 struct inode *ip;
Nathan Scott8285fb52006-06-09 17:07:12 +1000289 bhv_vattr_t vattr = { 0 };
Nathan Scott67fcaa72006-06-09 17:00:52 +1000290 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 xfs_acl_t *default_acl = NULL;
292 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
293 int error;
294
295 /*
296 * Irix uses Missed'em'V split, but doesn't want to see
297 * the upper 5 bits of (14bit) major.
298 */
Nathan Scott220b5282006-03-14 13:33:36 +1100299 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -EINVAL;
301
Nathan Scott220b5282006-03-14 13:33:36 +1100302 if (unlikely(test_default_acl && test_default_acl(dvp))) {
303 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
307 _ACL_FREE(default_acl);
308 default_acl = NULL;
309 }
310 }
311
Nathan Scott416c6d52006-03-14 14:00:51 +1100312 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 mode &= ~current->fs->umask;
314
Nathan Scott524fbf52006-03-14 14:07:53 +1100315 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
316 vattr.va_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 switch (mode & S_IFMT) {
319 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
Nathan Scott524fbf52006-03-14 14:07:53 +1100320 vattr.va_rdev = sysv_encode_dev(rdev);
321 vattr.va_mask |= XFS_AT_RDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /*FALLTHROUGH*/
323 case S_IFREG:
Nathan Scott67fcaa72006-06-09 17:00:52 +1000324 error = bhv_vop_create(dvp, dentry, &vattr, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 case S_IFDIR:
Nathan Scott67fcaa72006-06-09 17:00:52 +1000327 error = bhv_vop_mkdir(dvp, dentry, &vattr, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 default:
330 error = EINVAL;
331 break;
332 }
333
Nathan Scott220b5282006-03-14 13:33:36 +1100334 if (unlikely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100335 error = xfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100336 if (error)
Nathan Scott416c6d52006-03-14 14:00:51 +1100337 xfs_cleanup_inode(dvp, vp, dentry, mode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100338 }
Nathan Scott446ada42006-01-11 15:35:44 +1100339
Nathan Scott220b5282006-03-14 13:33:36 +1100340 if (unlikely(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!error) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100342 error = _ACL_INHERIT(vp, &vattr, default_acl);
Nathan Scott220b5282006-03-14 13:33:36 +1100343 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 VMODIFY(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100345 else
Nathan Scott416c6d52006-03-14 14:00:51 +1100346 xfs_cleanup_inode(dvp, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 _ACL_FREE(default_acl);
349 }
350
Nathan Scott220b5282006-03-14 13:33:36 +1100351 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ASSERT(vp);
Nathan Scottec86dc02006-03-17 17:25:36 +1100353 ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (S_ISCHR(mode) || S_ISBLK(mode))
356 ip->i_rdev = rdev;
357 else if (S_ISDIR(mode))
Nathan Scott524fbf52006-03-14 14:07:53 +1100358 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 d_instantiate(dentry, ip);
Nathan Scott524fbf52006-03-14 14:07:53 +1100360 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 return -error;
363}
364
365STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100366xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct inode *dir,
368 struct dentry *dentry,
369 int mode,
370 struct nameidata *nd)
371{
Nathan Scott416c6d52006-03-14 14:00:51 +1100372 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100376xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct inode *dir,
378 struct dentry *dentry,
379 int mode)
380{
Nathan Scott416c6d52006-03-14 14:00:51 +1100381 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100385xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct inode *dir,
387 struct dentry *dentry,
388 struct nameidata *nd)
389{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000390 bhv_vnode_t *vp = vn_from_inode(dir), *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int error;
392
393 if (dentry->d_name.len >= MAXNAMELEN)
394 return ERR_PTR(-ENAMETOOLONG);
395
Nathan Scott67fcaa72006-06-09 17:00:52 +1000396 error = bhv_vop_lookup(vp, dentry, &cvp, 0, NULL, NULL);
397 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (unlikely(error != ENOENT))
399 return ERR_PTR(-error);
400 d_add(dentry, NULL);
401 return NULL;
402 }
403
Nathan Scottec86dc02006-03-17 17:25:36 +1100404 return d_splice_alias(vn_to_inode(cvp), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100408xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct dentry *old_dentry,
410 struct inode *dir,
411 struct dentry *dentry)
412{
413 struct inode *ip; /* inode of guy being linked to */
Nathan Scott67fcaa72006-06-09 17:00:52 +1000414 bhv_vnode_t *tdvp; /* target directory for new name/link */
415 bhv_vnode_t *vp; /* vp of name being linked */
Nathan Scott8285fb52006-06-09 17:07:12 +1000416 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int error;
418
419 ip = old_dentry->d_inode; /* inode being linked to */
Nathan Scottec86dc02006-03-17 17:25:36 +1100420 tdvp = vn_from_inode(dir);
421 vp = vn_from_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Nathan Scott97dfd702006-06-27 16:13:46 +1000423 VN_HOLD(vp);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000424 error = bhv_vop_link(tdvp, vp, dentry, NULL);
Nathan Scott97dfd702006-06-27 16:13:46 +1000425 if (unlikely(error)) {
426 VN_RELE(vp);
427 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 VMODIFY(tdvp);
Nathan Scott524fbf52006-03-14 14:07:53 +1100429 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 d_instantiate(dentry, ip);
431 }
432 return -error;
433}
434
435STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100436xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct inode *dir,
438 struct dentry *dentry)
439{
440 struct inode *inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000441 bhv_vnode_t *dvp; /* directory containing name to remove */
Nathan Scott8285fb52006-06-09 17:07:12 +1000442 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int error;
444
445 inode = dentry->d_inode;
Nathan Scottec86dc02006-03-17 17:25:36 +1100446 dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Nathan Scott67fcaa72006-06-09 17:00:52 +1000448 error = bhv_vop_remove(dvp, dentry, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100449 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100450 xfs_validate_fields(dir, &vattr); /* size needs update */
451 xfs_validate_fields(inode, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -error;
454}
455
456STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100457xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct inode *dir,
459 struct dentry *dentry,
460 const char *symname)
461{
462 struct inode *ip;
Nathan Scott8285fb52006-06-09 17:07:12 +1000463 bhv_vattr_t va = { 0 };
Nathan Scott67fcaa72006-06-09 17:00:52 +1000464 bhv_vnode_t *dvp; /* directory containing name of symlink */
465 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int error;
467
Nathan Scottec86dc02006-03-17 17:25:36 +1100468 dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 cvp = NULL;
470
Nathan Scott67fcaa72006-06-09 17:00:52 +1000471 va.va_mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000472 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000473 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Nathan Scott67fcaa72006-06-09 17:00:52 +1000475 error = bhv_vop_symlink(dvp, dentry, &va, (char *)symname, &cvp, NULL);
Nathan Scott54245702006-01-16 15:54:05 +1100476 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100477 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100478 if (likely(!error)) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100479 ip = vn_to_inode(cvp);
Nathan Scott54245702006-01-16 15:54:05 +1100480 d_instantiate(dentry, ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000481 xfs_validate_fields(dir, &va);
482 xfs_validate_fields(ip, &va);
Nathan Scottce9d37c2006-03-17 17:26:34 +1100483 } else {
484 xfs_cleanup_inode(dvp, cvp, dentry, 0);
Nathan Scott54245702006-01-16 15:54:05 +1100485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 return -error;
488}
489
490STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100491xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct inode *dir,
493 struct dentry *dentry)
494{
495 struct inode *inode = dentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000496 bhv_vnode_t *dvp = vn_from_inode(dir);
Nathan Scott8285fb52006-06-09 17:07:12 +1000497 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int error;
499
Nathan Scott67fcaa72006-06-09 17:00:52 +1000500 error = bhv_vop_rmdir(dvp, dentry, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100501 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100502 xfs_validate_fields(inode, &vattr);
503 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 return -error;
506}
507
508STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100509xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct inode *odir,
511 struct dentry *odentry,
512 struct inode *ndir,
513 struct dentry *ndentry)
514{
515 struct inode *new_inode = ndentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000516 bhv_vnode_t *fvp; /* from directory */
517 bhv_vnode_t *tvp; /* target directory */
Nathan Scott8285fb52006-06-09 17:07:12 +1000518 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int error;
520
Nathan Scottec86dc02006-03-17 17:25:36 +1100521 fvp = vn_from_inode(odir);
522 tvp = vn_from_inode(ndir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Nathan Scott67fcaa72006-06-09 17:00:52 +1000524 error = bhv_vop_rename(fvp, odentry, tvp, ndentry, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100525 if (likely(!error)) {
526 if (new_inode)
Nathan Scott524fbf52006-03-14 14:07:53 +1100527 xfs_validate_fields(new_inode, &vattr);
528 xfs_validate_fields(odir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100529 if (ndir != odir)
Nathan Scott524fbf52006-03-14 14:07:53 +1100530 xfs_validate_fields(ndir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100531 }
Nathan Scott220b5282006-03-14 13:33:36 +1100532 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
536 * careful here - this function can get called recursively, so
537 * we need to be very careful about how much stack we use.
538 * uio is kmalloced for this reason...
539 */
Al Viro008b1502005-08-20 00:17:39 +0100540STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100541xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct dentry *dentry,
543 struct nameidata *nd)
544{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000545 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000547 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700549 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000550 if (!link)
551 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000553 error = -bhv_vop_readlink(vp, link);
554 if (unlikely(error))
555 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100558 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000559
560 out_kfree:
561 kfree(link);
562 out_err:
563 nd_set_link(nd, ERR_PTR(error));
564 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Nathan Scottcde410a2005-09-05 11:47:01 +1000567STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100568xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000569 struct dentry *dentry,
570 struct nameidata *nd,
571 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Nathan Scottcde410a2005-09-05 11:47:01 +1000573 char *s = nd_get_link(nd);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!IS_ERR(s))
576 kfree(s);
577}
578
579#ifdef CONFIG_XFS_POSIX_ACL
580STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100581xfs_vn_permission(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct inode *inode,
583 int mode,
584 struct nameidata *nd)
585{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000586 return -bhv_vop_access(vn_from_inode(inode), mode << 6, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100589#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#endif
591
592STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100593xfs_vn_getattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct vfsmount *mnt,
595 struct dentry *dentry,
596 struct kstat *stat)
597{
598 struct inode *inode = dentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000599 bhv_vnode_t *vp = vn_from_inode(inode);
Nathan Scott69e23b92006-09-28 11:01:22 +1000600 bhv_vattr_t vattr = { .va_mask = XFS_AT_STAT };
601 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Nathan Scott69e23b92006-09-28 11:01:22 +1000603 error = bhv_vop_getattr(vp, &vattr, ATTR_LAZY, NULL);
604 if (likely(!error)) {
605 stat->size = i_size_read(inode);
606 stat->dev = inode->i_sb->s_dev;
607 stat->rdev = (vattr.va_rdev == 0) ? 0 :
608 MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
609 sysv_minor(vattr.va_rdev));
610 stat->mode = vattr.va_mode;
611 stat->nlink = vattr.va_nlink;
612 stat->uid = vattr.va_uid;
613 stat->gid = vattr.va_gid;
614 stat->ino = vattr.va_nodeid;
615 stat->atime = vattr.va_atime;
616 stat->mtime = vattr.va_mtime;
617 stat->ctime = vattr.va_ctime;
618 stat->blocks = vattr.va_nblocks;
619 stat->blksize = vattr.va_blocksize;
620 }
Nathan Scottb76963f2006-06-09 14:58:20 +1000621 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100625xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct dentry *dentry,
627 struct iattr *attr)
628{
629 struct inode *inode = dentry->d_inode;
630 unsigned int ia_valid = attr->ia_valid;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000631 bhv_vnode_t *vp = vn_from_inode(inode);
Nathan Scott8285fb52006-06-09 17:07:12 +1000632 bhv_vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int flags = 0;
634 int error;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (ia_valid & ATTR_UID) {
637 vattr.va_mask |= XFS_AT_UID;
638 vattr.va_uid = attr->ia_uid;
639 }
640 if (ia_valid & ATTR_GID) {
641 vattr.va_mask |= XFS_AT_GID;
642 vattr.va_gid = attr->ia_gid;
643 }
644 if (ia_valid & ATTR_SIZE) {
645 vattr.va_mask |= XFS_AT_SIZE;
646 vattr.va_size = attr->ia_size;
647 }
648 if (ia_valid & ATTR_ATIME) {
649 vattr.va_mask |= XFS_AT_ATIME;
650 vattr.va_atime = attr->ia_atime;
Nathan Scott8c0b5112006-04-11 15:12:45 +1000651 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 if (ia_valid & ATTR_MTIME) {
654 vattr.va_mask |= XFS_AT_MTIME;
655 vattr.va_mtime = attr->ia_mtime;
656 }
657 if (ia_valid & ATTR_CTIME) {
658 vattr.va_mask |= XFS_AT_CTIME;
659 vattr.va_ctime = attr->ia_ctime;
660 }
661 if (ia_valid & ATTR_MODE) {
662 vattr.va_mask |= XFS_AT_MODE;
663 vattr.va_mode = attr->ia_mode;
664 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
665 inode->i_mode &= ~S_ISGID;
666 }
667
668 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
669 flags |= ATTR_UTIME;
670#ifdef ATTR_NO_BLOCK
671 if ((ia_valid & ATTR_NO_BLOCK))
672 flags |= ATTR_NONBLOCK;
673#endif
674
Nathan Scott67fcaa72006-06-09 17:00:52 +1000675 error = bhv_vop_setattr(vp, &vattr, flags, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100676 if (likely(!error))
677 __vn_revalidate(vp, &vattr);
678 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100682xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct inode *inode)
684{
Nathan Scottc2536662006-03-29 10:44:40 +1000685 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100689xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct dentry *dentry,
691 const char *name,
692 const void *data,
693 size_t size,
694 int flags)
695{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000696 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 char *attr = (char *)name;
698 attrnames_t *namesp;
699 int xflags = 0;
700 int error;
701
702 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
703 if (!namesp)
704 return -EOPNOTSUPP;
705 attr += namesp->attr_namelen;
706 error = namesp->attr_capable(vp, NULL);
707 if (error)
708 return error;
709
710 /* Convert Linux syscall to XFS internal ATTR flags */
711 if (flags & XATTR_CREATE)
712 xflags |= ATTR_CREATE;
713 if (flags & XATTR_REPLACE)
714 xflags |= ATTR_REPLACE;
715 xflags |= namesp->attr_flag;
716 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
717}
718
719STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100720xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct dentry *dentry,
722 const char *name,
723 void *data,
724 size_t size)
725{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000726 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 char *attr = (char *)name;
728 attrnames_t *namesp;
729 int xflags = 0;
730 ssize_t error;
731
732 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
733 if (!namesp)
734 return -EOPNOTSUPP;
735 attr += namesp->attr_namelen;
736 error = namesp->attr_capable(vp, NULL);
737 if (error)
738 return error;
739
740 /* Convert Linux syscall to XFS internal ATTR flags */
741 if (!size) {
742 xflags |= ATTR_KERNOVAL;
743 data = NULL;
744 }
745 xflags |= namesp->attr_flag;
746 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
747}
748
749STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100750xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct dentry *dentry,
752 char *data,
753 size_t size)
754{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000755 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int error, xflags = ATTR_KERNAMELS;
757 ssize_t result;
758
759 if (!size)
760 xflags |= ATTR_KERNOVAL;
761 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
762
763 error = attr_generic_list(vp, data, size, xflags, &result);
764 if (error < 0)
765 return error;
766 return result;
767}
768
769STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100770xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct dentry *dentry,
772 const char *name)
773{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000774 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 char *attr = (char *)name;
776 attrnames_t *namesp;
777 int xflags = 0;
778 int error;
779
780 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
781 if (!namesp)
782 return -EOPNOTSUPP;
783 attr += namesp->attr_namelen;
784 error = namesp->attr_capable(vp, NULL);
785 if (error)
786 return error;
787 xflags |= namesp->attr_flag;
788 return namesp->attr_remove(vp, attr, xflags);
789}
790
791
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800792const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100793 .permission = xfs_vn_permission,
794 .truncate = xfs_vn_truncate,
795 .getattr = xfs_vn_getattr,
796 .setattr = xfs_vn_setattr,
797 .setxattr = xfs_vn_setxattr,
798 .getxattr = xfs_vn_getxattr,
799 .listxattr = xfs_vn_listxattr,
800 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801};
802
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800803const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100804 .create = xfs_vn_create,
805 .lookup = xfs_vn_lookup,
806 .link = xfs_vn_link,
807 .unlink = xfs_vn_unlink,
808 .symlink = xfs_vn_symlink,
809 .mkdir = xfs_vn_mkdir,
810 .rmdir = xfs_vn_rmdir,
811 .mknod = xfs_vn_mknod,
812 .rename = xfs_vn_rename,
813 .permission = xfs_vn_permission,
814 .getattr = xfs_vn_getattr,
815 .setattr = xfs_vn_setattr,
816 .setxattr = xfs_vn_setxattr,
817 .getxattr = xfs_vn_getxattr,
818 .listxattr = xfs_vn_listxattr,
819 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800822const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100824 .follow_link = xfs_vn_follow_link,
825 .put_link = xfs_vn_put_link,
826 .permission = xfs_vn_permission,
827 .getattr = xfs_vn_getattr,
828 .setattr = xfs_vn_setattr,
829 .setxattr = xfs_vn_setxattr,
830 .getxattr = xfs_vn_getxattr,
831 .listxattr = xfs_vn_listxattr,
832 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833};