blob: 64c3395074dea86bfcd16c4c045cb8c0dc529714 [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"
26#include "xfs_dir.h"
27#include "xfs_dir2.h"
28#include "xfs_alloc.h"
29#include "xfs_dmapi.h"
30#include "xfs_quota.h"
31#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir_sf.h"
36#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_dinode.h"
39#include "xfs_inode.h"
40#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_btree.h"
42#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_rtalloc.h"
44#include "xfs_error.h"
45#include "xfs_itable.h"
46#include "xfs_rw.h"
47#include "xfs_acl.h"
48#include "xfs_cap.h"
49#include "xfs_mac.h"
50#include "xfs_attr.h"
51#include "xfs_buf_item.h"
52#include "xfs_utils.h"
53
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080054#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/xattr.h>
56#include <linux/namei.h>
Nathan Scott446ada42006-01-11 15:35:44 +110057#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Nathan Scott4aeb6642005-11-02 11:43:58 +110059/*
Christoph Hellwig75e17b32006-01-11 20:58:44 +110060 * Get a XFS inode from a given vnode.
61 */
62xfs_inode_t *
63xfs_vtoi(
64 struct vnode *vp)
65{
66 bhv_desc_t *bdp;
67
68 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
69 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
70 if (unlikely(bdp == NULL))
71 return NULL;
72 return XFS_BHVTOI(bdp);
73}
74
75/*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110076 * Bring the atime in the XFS inode uptodate.
77 * Used before logging the inode to disk or when the Linux inode goes away.
78 */
79void
80xfs_synchronize_atime(
81 xfs_inode_t *ip)
82{
83 vnode_t *vp;
84
85 vp = XFS_ITOV_NULL(ip);
86 if (vp) {
87 struct inode *inode = &vp->v_inode;
88 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
89 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
90 }
91}
92
93/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110094 * Change the requested timestamp in the given inode.
95 * We don't lock across timestamp updates, and we don't log them but
96 * we do record the fact that there is dirty information in core.
97 *
98 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
99 * with XFS_ICHGTIME_ACC to be sure that access time
100 * update will take. Calling first with XFS_ICHGTIME_ACC
101 * and then XFS_ICHGTIME_MOD may fail to modify the access
102 * timestamp if the filesystem is mounted noacctm.
103 */
104void
105xfs_ichgtime(
106 xfs_inode_t *ip,
107 int flags)
108{
Nathan Scottec86dc02006-03-17 17:25:36 +1100109 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +1100110 timespec_t tv;
111
Nathan Scott4aeb6642005-11-02 11:43:58 +1100112 nanotime(&tv);
113 if (flags & XFS_ICHGTIME_MOD) {
114 inode->i_mtime = tv;
115 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
116 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
117 }
118 if (flags & XFS_ICHGTIME_ACC) {
119 inode->i_atime = tv;
120 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
121 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
122 }
123 if (flags & XFS_ICHGTIME_CHG) {
124 inode->i_ctime = tv;
125 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
126 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
127 }
128
129 /*
130 * We update the i_update_core field _after_ changing
131 * the timestamps in order to coordinate properly with
132 * xfs_iflush() so that we don't lose timestamp updates.
133 * This keeps us from having to hold the inode lock
134 * while doing this. We use the SYNCHRONIZE macro to
135 * ensure that the compiler does not reorder the update
136 * of i_update_core above the timestamp updates above.
137 */
138 SYNCHRONIZE();
139 ip->i_update_core = 1;
140 if (!(inode->i_state & I_LOCK))
141 mark_inode_dirty_sync(inode);
142}
143
144/*
145 * Variant on the above which avoids querying the system clock
146 * in situations where we know the Linux inode timestamps have
147 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100148 */
149void
150xfs_ichgtime_fast(
151 xfs_inode_t *ip,
152 struct inode *inode,
153 int flags)
154{
155 timespec_t *tvp;
156
157 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100158 * Atime updates for read() & friends are handled lazily now, and
159 * explicit updates must go through xfs_ichgtime()
160 */
161 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
162
163 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100164 * We're not supposed to change timestamps in readonly-mounted
165 * filesystems. Throw it away if anyone asks us.
166 */
167 if (unlikely(IS_RDONLY(inode)))
168 return;
169
Nathan Scott4aeb6642005-11-02 11:43:58 +1100170 if (flags & XFS_ICHGTIME_MOD) {
171 tvp = &inode->i_mtime;
172 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
173 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
174 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100175 if (flags & XFS_ICHGTIME_CHG) {
176 tvp = &inode->i_ctime;
177 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
178 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
179 }
180
181 /*
182 * We update the i_update_core field _after_ changing
183 * the timestamps in order to coordinate properly with
184 * xfs_iflush() so that we don't lose timestamp updates.
185 * This keeps us from having to hold the inode lock
186 * while doing this. We use the SYNCHRONIZE macro to
187 * ensure that the compiler does not reorder the update
188 * of i_update_core above the timestamp updates above.
189 */
190 SYNCHRONIZE();
191 ip->i_update_core = 1;
192 if (!(inode->i_state & I_LOCK))
193 mark_inode_dirty_sync(inode);
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197/*
198 * Pull the link count and size up from the xfs inode to the linux inode
199 */
200STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100201xfs_validate_fields(
Nathan Scott220b5282006-03-14 13:33:36 +1100202 struct inode *ip,
203 struct vattr *vattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Nathan Scottec86dc02006-03-17 17:25:36 +1100205 vnode_t *vp = vn_from_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int error;
207
Nathan Scott220b5282006-03-14 13:33:36 +1100208 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
209 VOP_GETATTR(vp, vattr, ATTR_LAZY, NULL, error);
210 if (likely(!error)) {
211 ip->i_nlink = vattr->va_nlink;
212 ip->i_blocks = vattr->va_nblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Nathan Scott220b5282006-03-14 13:33:36 +1100214 /* we're under i_sem so i_size can't change under us */
215 if (i_size_read(ip) != vattr->va_size)
216 i_size_write(ip, vattr->va_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218}
219
220/*
Nathan Scott446ada42006-01-11 15:35:44 +1100221 * Hook in SELinux. This is not quite correct yet, what we really need
222 * here (as we do for default ACLs) is a mechanism by which creation of
223 * these attrs can be journalled at inode creation time (along with the
224 * inode, of course, such that log replay can't cause these to be lost).
225 */
226STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100227xfs_init_security(
Nathan Scott446ada42006-01-11 15:35:44 +1100228 struct vnode *vp,
229 struct inode *dir)
230{
Nathan Scottec86dc02006-03-17 17:25:36 +1100231 struct inode *ip = vn_to_inode(vp);
Nathan Scott446ada42006-01-11 15:35:44 +1100232 size_t length;
233 void *value;
234 char *name;
235 int error;
236
237 error = security_inode_init_security(ip, dir, &name, &value, &length);
238 if (error) {
239 if (error == -EOPNOTSUPP)
240 return 0;
241 return -error;
242 }
243
244 VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
245 if (!error)
246 VMODIFY(vp);
247
248 kfree(name);
249 kfree(value);
250 return error;
251}
252
253/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * Determine whether a process has a valid fs_struct (kernel daemons
255 * like knfsd don't have an fs_struct).
256 *
257 * XXX(hch): nfsd is broken, better fix it instead.
258 */
259STATIC inline int
Nathan Scott416c6d52006-03-14 14:00:51 +1100260xfs_has_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 return (task->fs != init_task.fs);
263}
264
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100265STATIC inline void
Nathan Scott416c6d52006-03-14 14:00:51 +1100266xfs_cleanup_inode(
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100267 vnode_t *dvp,
268 vnode_t *vp,
Nathan Scott220b5282006-03-14 13:33:36 +1100269 struct dentry *dentry,
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100270 int mode)
271{
272 struct dentry teardown = {};
Nathan Scott220b5282006-03-14 13:33:36 +1100273 int error;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100274
275 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100276 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100277 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100278 * ENOSPC can hit here, among other things.
279 */
Nathan Scottec86dc02006-03-17 17:25:36 +1100280 teardown.d_inode = vn_to_inode(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100281 teardown.d_name = dentry->d_name;
282
283 if (S_ISDIR(mode))
Nathan Scott220b5282006-03-14 13:33:36 +1100284 VOP_RMDIR(dvp, &teardown, NULL, error);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100285 else
Nathan Scott220b5282006-03-14 13:33:36 +1100286 VOP_REMOVE(dvp, &teardown, NULL, error);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100287 VN_RELE(vp);
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100291xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct inode *dir,
293 struct dentry *dentry,
294 int mode,
295 dev_t rdev)
296{
297 struct inode *ip;
Nathan Scott524fbf52006-03-14 14:07:53 +1100298 vattr_t vattr = { 0 };
Nathan Scottec86dc02006-03-17 17:25:36 +1100299 vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 xfs_acl_t *default_acl = NULL;
301 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
302 int error;
303
304 /*
305 * Irix uses Missed'em'V split, but doesn't want to see
306 * the upper 5 bits of (14bit) major.
307 */
Nathan Scott220b5282006-03-14 13:33:36 +1100308 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -EINVAL;
310
Nathan Scott220b5282006-03-14 13:33:36 +1100311 if (unlikely(test_default_acl && test_default_acl(dvp))) {
312 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
316 _ACL_FREE(default_acl);
317 default_acl = NULL;
318 }
319 }
320
Nathan Scott416c6d52006-03-14 14:00:51 +1100321 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 mode &= ~current->fs->umask;
323
Nathan Scott524fbf52006-03-14 14:07:53 +1100324 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
325 vattr.va_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 switch (mode & S_IFMT) {
328 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
Nathan Scott524fbf52006-03-14 14:07:53 +1100329 vattr.va_rdev = sysv_encode_dev(rdev);
330 vattr.va_mask |= XFS_AT_RDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*FALLTHROUGH*/
332 case S_IFREG:
Nathan Scott524fbf52006-03-14 14:07:53 +1100333 VOP_CREATE(dvp, dentry, &vattr, &vp, NULL, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
335 case S_IFDIR:
Nathan Scott524fbf52006-03-14 14:07:53 +1100336 VOP_MKDIR(dvp, dentry, &vattr, &vp, NULL, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
338 default:
339 error = EINVAL;
340 break;
341 }
342
Nathan Scott220b5282006-03-14 13:33:36 +1100343 if (unlikely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100344 error = xfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100345 if (error)
Nathan Scott416c6d52006-03-14 14:00:51 +1100346 xfs_cleanup_inode(dvp, vp, dentry, mode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100347 }
Nathan Scott446ada42006-01-11 15:35:44 +1100348
Nathan Scott220b5282006-03-14 13:33:36 +1100349 if (unlikely(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!error) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100351 error = _ACL_INHERIT(vp, &vattr, default_acl);
Nathan Scott220b5282006-03-14 13:33:36 +1100352 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 VMODIFY(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100354 else
Nathan Scott416c6d52006-03-14 14:00:51 +1100355 xfs_cleanup_inode(dvp, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 _ACL_FREE(default_acl);
358 }
359
Nathan Scott220b5282006-03-14 13:33:36 +1100360 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ASSERT(vp);
Nathan Scottec86dc02006-03-17 17:25:36 +1100362 ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (S_ISCHR(mode) || S_ISBLK(mode))
365 ip->i_rdev = rdev;
366 else if (S_ISDIR(mode))
Nathan Scott524fbf52006-03-14 14:07:53 +1100367 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 d_instantiate(dentry, ip);
Nathan Scott524fbf52006-03-14 14:07:53 +1100369 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 return -error;
372}
373
374STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100375xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct inode *dir,
377 struct dentry *dentry,
378 int mode,
379 struct nameidata *nd)
380{
Nathan Scott416c6d52006-03-14 14:00:51 +1100381 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100385xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct inode *dir,
387 struct dentry *dentry,
388 int mode)
389{
Nathan Scott416c6d52006-03-14 14:00:51 +1100390 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100394xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct inode *dir,
396 struct dentry *dentry,
397 struct nameidata *nd)
398{
Nathan Scottec86dc02006-03-17 17:25:36 +1100399 struct vnode *vp = vn_from_inode(dir), *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int error;
401
402 if (dentry->d_name.len >= MAXNAMELEN)
403 return ERR_PTR(-ENAMETOOLONG);
404
405 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
406 if (error) {
407 if (unlikely(error != ENOENT))
408 return ERR_PTR(-error);
409 d_add(dentry, NULL);
410 return NULL;
411 }
412
Nathan Scottec86dc02006-03-17 17:25:36 +1100413 return d_splice_alias(vn_to_inode(cvp), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100417xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct dentry *old_dentry,
419 struct inode *dir,
420 struct dentry *dentry)
421{
422 struct inode *ip; /* inode of guy being linked to */
423 vnode_t *tdvp; /* target directory for new name/link */
424 vnode_t *vp; /* vp of name being linked */
Nathan Scott524fbf52006-03-14 14:07:53 +1100425 vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int error;
427
428 ip = old_dentry->d_inode; /* inode being linked to */
429 if (S_ISDIR(ip->i_mode))
430 return -EPERM;
431
Nathan Scottec86dc02006-03-17 17:25:36 +1100432 tdvp = vn_from_inode(dir);
433 vp = vn_from_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 VOP_LINK(tdvp, vp, dentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100436 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 VMODIFY(tdvp);
438 VN_HOLD(vp);
Nathan Scott524fbf52006-03-14 14:07:53 +1100439 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 d_instantiate(dentry, ip);
441 }
442 return -error;
443}
444
445STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100446xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct inode *dir,
448 struct dentry *dentry)
449{
450 struct inode *inode;
451 vnode_t *dvp; /* directory containing name to remove */
Nathan Scott524fbf52006-03-14 14:07:53 +1100452 vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int error;
454
455 inode = dentry->d_inode;
Nathan Scottec86dc02006-03-17 17:25:36 +1100456 dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 VOP_REMOVE(dvp, dentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100459 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100460 xfs_validate_fields(dir, &vattr); /* size needs update */
461 xfs_validate_fields(inode, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return -error;
464}
465
466STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100467xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct inode *dir,
469 struct dentry *dentry,
470 const char *symname)
471{
472 struct inode *ip;
Nathan Scott524fbf52006-03-14 14:07:53 +1100473 vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 vnode_t *dvp; /* directory containing name of symlink */
475 vnode_t *cvp; /* used to lookup symlink to put in dentry */
476 int error;
477
Nathan Scottec86dc02006-03-17 17:25:36 +1100478 dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 cvp = NULL;
480
Nathan Scott524fbf52006-03-14 14:07:53 +1100481 vattr.va_mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000482 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Nathan Scott524fbf52006-03-14 14:07:53 +1100483 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 error = 0;
Nathan Scott524fbf52006-03-14 14:07:53 +1100486 VOP_SYMLINK(dvp, dentry, &vattr, (char *)symname, &cvp, NULL, error);
Nathan Scott54245702006-01-16 15:54:05 +1100487 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100488 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100489 if (likely(!error)) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100490 ip = vn_to_inode(cvp);
Nathan Scott54245702006-01-16 15:54:05 +1100491 d_instantiate(dentry, ip);
Nathan Scott524fbf52006-03-14 14:07:53 +1100492 xfs_validate_fields(dir, &vattr);
493 xfs_validate_fields(ip, &vattr);
Nathan Scott54245702006-01-16 15:54:05 +1100494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 return -error;
497}
498
499STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100500xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct inode *dir,
502 struct dentry *dentry)
503{
504 struct inode *inode = dentry->d_inode;
Nathan Scottec86dc02006-03-17 17:25:36 +1100505 vnode_t *dvp = vn_from_inode(dir);
Nathan Scott524fbf52006-03-14 14:07:53 +1100506 vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int error;
508
509 VOP_RMDIR(dvp, dentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100510 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100511 xfs_validate_fields(inode, &vattr);
512 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 return -error;
515}
516
517STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100518xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct inode *odir,
520 struct dentry *odentry,
521 struct inode *ndir,
522 struct dentry *ndentry)
523{
524 struct inode *new_inode = ndentry->d_inode;
525 vnode_t *fvp; /* from directory */
526 vnode_t *tvp; /* target directory */
Nathan Scott524fbf52006-03-14 14:07:53 +1100527 vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int error;
529
Nathan Scottec86dc02006-03-17 17:25:36 +1100530 fvp = vn_from_inode(odir);
531 tvp = vn_from_inode(ndir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100534 if (likely(!error)) {
535 if (new_inode)
Nathan Scott524fbf52006-03-14 14:07:53 +1100536 xfs_validate_fields(new_inode, &vattr);
537 xfs_validate_fields(odir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100538 if (ndir != odir)
Nathan Scott524fbf52006-03-14 14:07:53 +1100539 xfs_validate_fields(ndir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100540 }
Nathan Scott220b5282006-03-14 13:33:36 +1100541 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
545 * careful here - this function can get called recursively, so
546 * we need to be very careful about how much stack we use.
547 * uio is kmalloced for this reason...
548 */
Al Viro008b1502005-08-20 00:17:39 +0100549STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100550xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct dentry *dentry,
552 struct nameidata *nd)
553{
554 vnode_t *vp;
555 uio_t *uio;
556 iovec_t iov;
557 int error;
558 char *link;
559
560 ASSERT(dentry);
561 ASSERT(nd);
562
Nathan Scott0d1335b2006-01-12 10:32:51 +1100563 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!link) {
565 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100566 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
570 if (!uio) {
571 kfree(link);
572 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100573 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Nathan Scottec86dc02006-03-17 17:25:36 +1100576 vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 iov.iov_base = link;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100579 iov.iov_len = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 uio->uio_iov = &iov;
582 uio->uio_offset = 0;
583 uio->uio_segflg = UIO_SYSSPACE;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100584 uio->uio_resid = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 uio->uio_iovcnt = 1;
586
587 VOP_READLINK(vp, uio, 0, NULL, error);
588 if (error) {
589 kfree(link);
590 link = ERR_PTR(-error);
591 } else {
Nathan Scott0d1335b2006-01-12 10:32:51 +1100592 link[MAXPATHLEN - uio->uio_resid] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 kfree(uio);
595
596 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100597 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Nathan Scottcde410a2005-09-05 11:47:01 +1000600STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100601xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000602 struct dentry *dentry,
603 struct nameidata *nd,
604 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Nathan Scottcde410a2005-09-05 11:47:01 +1000606 char *s = nd_get_link(nd);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!IS_ERR(s))
609 kfree(s);
610}
611
612#ifdef CONFIG_XFS_POSIX_ACL
613STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100614xfs_vn_permission(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct inode *inode,
616 int mode,
617 struct nameidata *nd)
618{
Nathan Scottec86dc02006-03-17 17:25:36 +1100619 vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int error;
621
622 mode <<= 6; /* convert from linux to vnode access bits */
623 VOP_ACCESS(vp, mode, NULL, error);
624 return -error;
625}
626#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100627#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#endif
629
630STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100631xfs_vn_getattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct vfsmount *mnt,
633 struct dentry *dentry,
634 struct kstat *stat)
635{
636 struct inode *inode = dentry->d_inode;
Nathan Scottec86dc02006-03-17 17:25:36 +1100637 vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int error = 0;
639
640 if (unlikely(vp->v_flag & VMODIFIED))
641 error = vn_revalidate(vp);
642 if (!error)
643 generic_fillattr(inode, stat);
644 return 0;
645}
646
647STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100648xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct dentry *dentry,
650 struct iattr *attr)
651{
652 struct inode *inode = dentry->d_inode;
653 unsigned int ia_valid = attr->ia_valid;
Nathan Scottec86dc02006-03-17 17:25:36 +1100654 vnode_t *vp = vn_from_inode(inode);
Nathan Scott220b5282006-03-14 13:33:36 +1100655 vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int flags = 0;
657 int error;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (ia_valid & ATTR_UID) {
660 vattr.va_mask |= XFS_AT_UID;
661 vattr.va_uid = attr->ia_uid;
662 }
663 if (ia_valid & ATTR_GID) {
664 vattr.va_mask |= XFS_AT_GID;
665 vattr.va_gid = attr->ia_gid;
666 }
667 if (ia_valid & ATTR_SIZE) {
668 vattr.va_mask |= XFS_AT_SIZE;
669 vattr.va_size = attr->ia_size;
670 }
671 if (ia_valid & ATTR_ATIME) {
672 vattr.va_mask |= XFS_AT_ATIME;
673 vattr.va_atime = attr->ia_atime;
Nathan Scott9bd6f132006-02-07 20:27:44 +1100674 if (ia_valid & ATTR_ATIME_SET)
675 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 if (ia_valid & ATTR_MTIME) {
678 vattr.va_mask |= XFS_AT_MTIME;
679 vattr.va_mtime = attr->ia_mtime;
680 }
681 if (ia_valid & ATTR_CTIME) {
682 vattr.va_mask |= XFS_AT_CTIME;
683 vattr.va_ctime = attr->ia_ctime;
684 }
685 if (ia_valid & ATTR_MODE) {
686 vattr.va_mask |= XFS_AT_MODE;
687 vattr.va_mode = attr->ia_mode;
688 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
689 inode->i_mode &= ~S_ISGID;
690 }
691
692 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
693 flags |= ATTR_UTIME;
694#ifdef ATTR_NO_BLOCK
695 if ((ia_valid & ATTR_NO_BLOCK))
696 flags |= ATTR_NONBLOCK;
697#endif
698
699 VOP_SETATTR(vp, &vattr, flags, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100700 if (likely(!error))
701 __vn_revalidate(vp, &vattr);
702 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100706xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct inode *inode)
708{
Nathan Scotte4c573b2006-03-14 13:54:26 +1100709 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100713xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct dentry *dentry,
715 const char *name,
716 const void *data,
717 size_t size,
718 int flags)
719{
Nathan Scottec86dc02006-03-17 17:25:36 +1100720 vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 char *attr = (char *)name;
722 attrnames_t *namesp;
723 int xflags = 0;
724 int error;
725
726 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
727 if (!namesp)
728 return -EOPNOTSUPP;
729 attr += namesp->attr_namelen;
730 error = namesp->attr_capable(vp, NULL);
731 if (error)
732 return error;
733
734 /* Convert Linux syscall to XFS internal ATTR flags */
735 if (flags & XATTR_CREATE)
736 xflags |= ATTR_CREATE;
737 if (flags & XATTR_REPLACE)
738 xflags |= ATTR_REPLACE;
739 xflags |= namesp->attr_flag;
740 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
741}
742
743STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100744xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct dentry *dentry,
746 const char *name,
747 void *data,
748 size_t size)
749{
Nathan Scottec86dc02006-03-17 17:25:36 +1100750 vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 char *attr = (char *)name;
752 attrnames_t *namesp;
753 int xflags = 0;
754 ssize_t error;
755
756 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
757 if (!namesp)
758 return -EOPNOTSUPP;
759 attr += namesp->attr_namelen;
760 error = namesp->attr_capable(vp, NULL);
761 if (error)
762 return error;
763
764 /* Convert Linux syscall to XFS internal ATTR flags */
765 if (!size) {
766 xflags |= ATTR_KERNOVAL;
767 data = NULL;
768 }
769 xflags |= namesp->attr_flag;
770 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
771}
772
773STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100774xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct dentry *dentry,
776 char *data,
777 size_t size)
778{
Nathan Scottec86dc02006-03-17 17:25:36 +1100779 vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int error, xflags = ATTR_KERNAMELS;
781 ssize_t result;
782
783 if (!size)
784 xflags |= ATTR_KERNOVAL;
785 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
786
787 error = attr_generic_list(vp, data, size, xflags, &result);
788 if (error < 0)
789 return error;
790 return result;
791}
792
793STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100794xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct dentry *dentry,
796 const char *name)
797{
Nathan Scottec86dc02006-03-17 17:25:36 +1100798 vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 char *attr = (char *)name;
800 attrnames_t *namesp;
801 int xflags = 0;
802 int error;
803
804 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
805 if (!namesp)
806 return -EOPNOTSUPP;
807 attr += namesp->attr_namelen;
808 error = namesp->attr_capable(vp, NULL);
809 if (error)
810 return error;
811 xflags |= namesp->attr_flag;
812 return namesp->attr_remove(vp, attr, xflags);
813}
814
815
Nathan Scott416c6d52006-03-14 14:00:51 +1100816struct inode_operations xfs_inode_operations = {
817 .permission = xfs_vn_permission,
818 .truncate = xfs_vn_truncate,
819 .getattr = xfs_vn_getattr,
820 .setattr = xfs_vn_setattr,
821 .setxattr = xfs_vn_setxattr,
822 .getxattr = xfs_vn_getxattr,
823 .listxattr = xfs_vn_listxattr,
824 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825};
826
Nathan Scott416c6d52006-03-14 14:00:51 +1100827struct inode_operations xfs_dir_inode_operations = {
828 .create = xfs_vn_create,
829 .lookup = xfs_vn_lookup,
830 .link = xfs_vn_link,
831 .unlink = xfs_vn_unlink,
832 .symlink = xfs_vn_symlink,
833 .mkdir = xfs_vn_mkdir,
834 .rmdir = xfs_vn_rmdir,
835 .mknod = xfs_vn_mknod,
836 .rename = xfs_vn_rename,
837 .permission = xfs_vn_permission,
838 .getattr = xfs_vn_getattr,
839 .setattr = xfs_vn_setattr,
840 .setxattr = xfs_vn_setxattr,
841 .getxattr = xfs_vn_getxattr,
842 .listxattr = xfs_vn_listxattr,
843 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844};
845
Nathan Scott416c6d52006-03-14 14:00:51 +1100846struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100848 .follow_link = xfs_vn_follow_link,
849 .put_link = xfs_vn_put_link,
850 .permission = xfs_vn_permission,
851 .getattr = xfs_vn_getattr,
852 .setattr = xfs_vn_setattr,
853 .setxattr = xfs_vn_setxattr,
854 .getxattr = xfs_vn_getxattr,
855 .listxattr = xfs_vn_listxattr,
856 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};