blob: 93b9e6e43f20a105661d659943719768af8cd8e5 [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{
109 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
110 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{
205 vnode_t *vp = LINVFS_GET_VP(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{
231 struct inode *ip = LINVFS_GET_IP(vp);
232 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 */
280 teardown.d_inode = LINVFS_GET_IP(vp);
281 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 Scott220b5282006-03-14 13:33:36 +1100298 vattr_t *vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
300 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 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
312 if (unlikely(!vattr))
313 return -ENOMEM;
314
315 if (unlikely(test_default_acl && test_default_acl(dvp))) {
316 if (!_ACL_ALLOC(default_acl)) {
317 kfree(vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
321 _ACL_FREE(default_acl);
322 default_acl = NULL;
323 }
324 }
325
Nathan Scott416c6d52006-03-14 14:00:51 +1100326 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 mode &= ~current->fs->umask;
328
Nathan Scott220b5282006-03-14 13:33:36 +1100329 memset(vattr, 0, sizeof(*vattr));
330 vattr->va_mask = XFS_AT_TYPE|XFS_AT_MODE;
331 vattr->va_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 switch (mode & S_IFMT) {
334 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
Nathan Scott220b5282006-03-14 13:33:36 +1100335 vattr->va_rdev = sysv_encode_dev(rdev);
336 vattr->va_mask |= XFS_AT_RDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /*FALLTHROUGH*/
338 case S_IFREG:
Nathan Scott220b5282006-03-14 13:33:36 +1100339 VOP_CREATE(dvp, dentry, vattr, &vp, NULL, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 break;
341 case S_IFDIR:
Nathan Scott220b5282006-03-14 13:33:36 +1100342 VOP_MKDIR(dvp, dentry, vattr, &vp, NULL, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 break;
344 default:
345 error = EINVAL;
346 break;
347 }
348
Nathan Scott220b5282006-03-14 13:33:36 +1100349 if (unlikely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100350 error = xfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100351 if (error)
Nathan Scott416c6d52006-03-14 14:00:51 +1100352 xfs_cleanup_inode(dvp, vp, dentry, mode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100353 }
Nathan Scott446ada42006-01-11 15:35:44 +1100354
Nathan Scott220b5282006-03-14 13:33:36 +1100355 if (unlikely(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!error) {
Nathan Scott220b5282006-03-14 13:33:36 +1100357 error = _ACL_INHERIT(vp, vattr, default_acl);
358 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 VMODIFY(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100360 else
Nathan Scott416c6d52006-03-14 14:00:51 +1100361 xfs_cleanup_inode(dvp, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 _ACL_FREE(default_acl);
364 }
365
Nathan Scott220b5282006-03-14 13:33:36 +1100366 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ASSERT(vp);
368 ip = LINVFS_GET_IP(vp);
369
370 if (S_ISCHR(mode) || S_ISBLK(mode))
371 ip->i_rdev = rdev;
372 else if (S_ISDIR(mode))
Nathan Scott416c6d52006-03-14 14:00:51 +1100373 xfs_validate_fields(ip, vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 d_instantiate(dentry, ip);
Nathan Scott416c6d52006-03-14 14:00:51 +1100375 xfs_validate_fields(dir, vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Nathan Scott220b5282006-03-14 13:33:36 +1100377 kfree(vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -error;
379}
380
381STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100382xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct inode *dir,
384 struct dentry *dentry,
385 int mode,
386 struct nameidata *nd)
387{
Nathan Scott416c6d52006-03-14 14:00:51 +1100388 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100392xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct inode *dir,
394 struct dentry *dentry,
395 int mode)
396{
Nathan Scott416c6d52006-03-14 14:00:51 +1100397 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100401xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct inode *dir,
403 struct dentry *dentry,
404 struct nameidata *nd)
405{
406 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
407 int error;
408
409 if (dentry->d_name.len >= MAXNAMELEN)
410 return ERR_PTR(-ENAMETOOLONG);
411
412 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
413 if (error) {
414 if (unlikely(error != ENOENT))
415 return ERR_PTR(-error);
416 d_add(dentry, NULL);
417 return NULL;
418 }
419
420 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
421}
422
423STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100424xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct dentry *old_dentry,
426 struct inode *dir,
427 struct dentry *dentry)
428{
429 struct inode *ip; /* inode of guy being linked to */
430 vnode_t *tdvp; /* target directory for new name/link */
431 vnode_t *vp; /* vp of name being linked */
Nathan Scott220b5282006-03-14 13:33:36 +1100432 vattr_t *vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int error;
434
435 ip = old_dentry->d_inode; /* inode being linked to */
436 if (S_ISDIR(ip->i_mode))
437 return -EPERM;
438
Nathan Scott220b5282006-03-14 13:33:36 +1100439 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
440 if (unlikely(!vattr))
441 return -ENOMEM;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 tdvp = LINVFS_GET_VP(dir);
444 vp = LINVFS_GET_VP(ip);
445
446 VOP_LINK(tdvp, vp, dentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100447 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 VMODIFY(tdvp);
449 VN_HOLD(vp);
Nathan Scott416c6d52006-03-14 14:00:51 +1100450 xfs_validate_fields(ip, vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 d_instantiate(dentry, ip);
452 }
Nathan Scott220b5282006-03-14 13:33:36 +1100453 kfree(vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -error;
455}
456
457STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100458xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct inode *dir,
460 struct dentry *dentry)
461{
462 struct inode *inode;
463 vnode_t *dvp; /* directory containing name to remove */
Nathan Scott220b5282006-03-14 13:33:36 +1100464 vattr_t *vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int error;
466
Nathan Scott220b5282006-03-14 13:33:36 +1100467 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
468 if (unlikely(!vattr))
469 return -ENOMEM;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 inode = dentry->d_inode;
472 dvp = LINVFS_GET_VP(dir);
473
474 VOP_REMOVE(dvp, dentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100475 if (likely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100476 xfs_validate_fields(dir, vattr); /* size needs update */
477 xfs_validate_fields(inode, vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Nathan Scott220b5282006-03-14 13:33:36 +1100479 kfree(vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -error;
481}
482
483STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100484xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct inode *dir,
486 struct dentry *dentry,
487 const char *symname)
488{
489 struct inode *ip;
Nathan Scott220b5282006-03-14 13:33:36 +1100490 vattr_t *vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 vnode_t *dvp; /* directory containing name of symlink */
492 vnode_t *cvp; /* used to lookup symlink to put in dentry */
493 int error;
494
495 dvp = LINVFS_GET_VP(dir);
496 cvp = NULL;
497
Nathan Scott220b5282006-03-14 13:33:36 +1100498 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
499 if (unlikely(!vattr))
500 return -ENOMEM;
501
502 memset(vattr, 0, sizeof(*vattr));
503 vattr->va_mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000504 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Nathan Scott220b5282006-03-14 13:33:36 +1100505 vattr->va_mask = XFS_AT_TYPE|XFS_AT_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 error = 0;
Nathan Scott220b5282006-03-14 13:33:36 +1100508 VOP_SYMLINK(dvp, dentry, vattr, (char *)symname, &cvp, NULL, error);
Nathan Scott54245702006-01-16 15:54:05 +1100509 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100510 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100511 if (likely(!error)) {
512 ip = LINVFS_GET_IP(cvp);
513 d_instantiate(dentry, ip);
Nathan Scott416c6d52006-03-14 14:00:51 +1100514 xfs_validate_fields(dir, vattr);
515 xfs_validate_fields(ip, vattr);
Nathan Scott54245702006-01-16 15:54:05 +1100516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Nathan Scott220b5282006-03-14 13:33:36 +1100518 kfree(vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -error;
520}
521
522STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100523xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct inode *dir,
525 struct dentry *dentry)
526{
527 struct inode *inode = dentry->d_inode;
528 vnode_t *dvp = LINVFS_GET_VP(dir);
Nathan Scott220b5282006-03-14 13:33:36 +1100529 vattr_t *vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int error;
531
Nathan Scott220b5282006-03-14 13:33:36 +1100532 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
533 if (unlikely(!vattr))
534 return -ENOMEM;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 VOP_RMDIR(dvp, dentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100537 if (likely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100538 xfs_validate_fields(inode, vattr);
539 xfs_validate_fields(dir, vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Nathan Scott220b5282006-03-14 13:33:36 +1100541 kfree(vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return -error;
543}
544
545STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100546xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct inode *odir,
548 struct dentry *odentry,
549 struct inode *ndir,
550 struct dentry *ndentry)
551{
552 struct inode *new_inode = ndentry->d_inode;
553 vnode_t *fvp; /* from directory */
554 vnode_t *tvp; /* target directory */
Nathan Scott220b5282006-03-14 13:33:36 +1100555 vattr_t *vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int error;
557
Nathan Scott220b5282006-03-14 13:33:36 +1100558 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
559 if (unlikely(!vattr))
560 return -ENOMEM;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 fvp = LINVFS_GET_VP(odir);
563 tvp = LINVFS_GET_VP(ndir);
564
565 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100566 if (likely(!error)) {
567 if (new_inode)
Nathan Scott416c6d52006-03-14 14:00:51 +1100568 xfs_validate_fields(new_inode, vattr);
569 xfs_validate_fields(odir, vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100570 if (ndir != odir)
Nathan Scott416c6d52006-03-14 14:00:51 +1100571 xfs_validate_fields(ndir, vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100572 }
573 kfree(vattr);
574 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * careful here - this function can get called recursively, so
579 * we need to be very careful about how much stack we use.
580 * uio is kmalloced for this reason...
581 */
Al Viro008b1502005-08-20 00:17:39 +0100582STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100583xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct dentry *dentry,
585 struct nameidata *nd)
586{
587 vnode_t *vp;
588 uio_t *uio;
589 iovec_t iov;
590 int error;
591 char *link;
592
593 ASSERT(dentry);
594 ASSERT(nd);
595
Nathan Scott0d1335b2006-01-12 10:32:51 +1100596 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!link) {
598 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100599 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
603 if (!uio) {
604 kfree(link);
605 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100606 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 vp = LINVFS_GET_VP(dentry->d_inode);
610
611 iov.iov_base = link;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100612 iov.iov_len = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 uio->uio_iov = &iov;
615 uio->uio_offset = 0;
616 uio->uio_segflg = UIO_SYSSPACE;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100617 uio->uio_resid = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 uio->uio_iovcnt = 1;
619
620 VOP_READLINK(vp, uio, 0, NULL, error);
621 if (error) {
622 kfree(link);
623 link = ERR_PTR(-error);
624 } else {
Nathan Scott0d1335b2006-01-12 10:32:51 +1100625 link[MAXPATHLEN - uio->uio_resid] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 kfree(uio);
628
629 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100630 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Nathan Scottcde410a2005-09-05 11:47:01 +1000633STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100634xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000635 struct dentry *dentry,
636 struct nameidata *nd,
637 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Nathan Scottcde410a2005-09-05 11:47:01 +1000639 char *s = nd_get_link(nd);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!IS_ERR(s))
642 kfree(s);
643}
644
645#ifdef CONFIG_XFS_POSIX_ACL
646STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100647xfs_vn_permission(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct inode *inode,
649 int mode,
650 struct nameidata *nd)
651{
652 vnode_t *vp = LINVFS_GET_VP(inode);
653 int error;
654
655 mode <<= 6; /* convert from linux to vnode access bits */
656 VOP_ACCESS(vp, mode, NULL, error);
657 return -error;
658}
659#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100660#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
662
663STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100664xfs_vn_getattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct vfsmount *mnt,
666 struct dentry *dentry,
667 struct kstat *stat)
668{
669 struct inode *inode = dentry->d_inode;
670 vnode_t *vp = LINVFS_GET_VP(inode);
671 int error = 0;
672
673 if (unlikely(vp->v_flag & VMODIFIED))
674 error = vn_revalidate(vp);
675 if (!error)
676 generic_fillattr(inode, stat);
677 return 0;
678}
679
680STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100681xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct dentry *dentry,
683 struct iattr *attr)
684{
685 struct inode *inode = dentry->d_inode;
686 unsigned int ia_valid = attr->ia_valid;
687 vnode_t *vp = LINVFS_GET_VP(inode);
Nathan Scott220b5282006-03-14 13:33:36 +1100688 vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int flags = 0;
690 int error;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (ia_valid & ATTR_UID) {
693 vattr.va_mask |= XFS_AT_UID;
694 vattr.va_uid = attr->ia_uid;
695 }
696 if (ia_valid & ATTR_GID) {
697 vattr.va_mask |= XFS_AT_GID;
698 vattr.va_gid = attr->ia_gid;
699 }
700 if (ia_valid & ATTR_SIZE) {
701 vattr.va_mask |= XFS_AT_SIZE;
702 vattr.va_size = attr->ia_size;
703 }
704 if (ia_valid & ATTR_ATIME) {
705 vattr.va_mask |= XFS_AT_ATIME;
706 vattr.va_atime = attr->ia_atime;
Nathan Scott9bd6f132006-02-07 20:27:44 +1100707 if (ia_valid & ATTR_ATIME_SET)
708 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710 if (ia_valid & ATTR_MTIME) {
711 vattr.va_mask |= XFS_AT_MTIME;
712 vattr.va_mtime = attr->ia_mtime;
713 }
714 if (ia_valid & ATTR_CTIME) {
715 vattr.va_mask |= XFS_AT_CTIME;
716 vattr.va_ctime = attr->ia_ctime;
717 }
718 if (ia_valid & ATTR_MODE) {
719 vattr.va_mask |= XFS_AT_MODE;
720 vattr.va_mode = attr->ia_mode;
721 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
722 inode->i_mode &= ~S_ISGID;
723 }
724
725 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
726 flags |= ATTR_UTIME;
727#ifdef ATTR_NO_BLOCK
728 if ((ia_valid & ATTR_NO_BLOCK))
729 flags |= ATTR_NONBLOCK;
730#endif
731
732 VOP_SETATTR(vp, &vattr, flags, NULL, error);
Nathan Scott220b5282006-03-14 13:33:36 +1100733 if (likely(!error))
734 __vn_revalidate(vp, &vattr);
735 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100739xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct inode *inode)
741{
Nathan Scotte4c573b2006-03-14 13:54:26 +1100742 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100746xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 struct dentry *dentry,
748 const char *name,
749 const void *data,
750 size_t size,
751 int flags)
752{
753 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
754 char *attr = (char *)name;
755 attrnames_t *namesp;
756 int xflags = 0;
757 int error;
758
759 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
760 if (!namesp)
761 return -EOPNOTSUPP;
762 attr += namesp->attr_namelen;
763 error = namesp->attr_capable(vp, NULL);
764 if (error)
765 return error;
766
767 /* Convert Linux syscall to XFS internal ATTR flags */
768 if (flags & XATTR_CREATE)
769 xflags |= ATTR_CREATE;
770 if (flags & XATTR_REPLACE)
771 xflags |= ATTR_REPLACE;
772 xflags |= namesp->attr_flag;
773 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
774}
775
776STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100777xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct dentry *dentry,
779 const char *name,
780 void *data,
781 size_t size)
782{
783 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
784 char *attr = (char *)name;
785 attrnames_t *namesp;
786 int xflags = 0;
787 ssize_t error;
788
789 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
790 if (!namesp)
791 return -EOPNOTSUPP;
792 attr += namesp->attr_namelen;
793 error = namesp->attr_capable(vp, NULL);
794 if (error)
795 return error;
796
797 /* Convert Linux syscall to XFS internal ATTR flags */
798 if (!size) {
799 xflags |= ATTR_KERNOVAL;
800 data = NULL;
801 }
802 xflags |= namesp->attr_flag;
803 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
804}
805
806STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100807xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct dentry *dentry,
809 char *data,
810 size_t size)
811{
812 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
813 int error, xflags = ATTR_KERNAMELS;
814 ssize_t result;
815
816 if (!size)
817 xflags |= ATTR_KERNOVAL;
818 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
819
820 error = attr_generic_list(vp, data, size, xflags, &result);
821 if (error < 0)
822 return error;
823 return result;
824}
825
826STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100827xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct dentry *dentry,
829 const char *name)
830{
831 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
832 char *attr = (char *)name;
833 attrnames_t *namesp;
834 int xflags = 0;
835 int error;
836
837 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
838 if (!namesp)
839 return -EOPNOTSUPP;
840 attr += namesp->attr_namelen;
841 error = namesp->attr_capable(vp, NULL);
842 if (error)
843 return error;
844 xflags |= namesp->attr_flag;
845 return namesp->attr_remove(vp, attr, xflags);
846}
847
848
Nathan Scott416c6d52006-03-14 14:00:51 +1100849struct inode_operations xfs_inode_operations = {
850 .permission = xfs_vn_permission,
851 .truncate = xfs_vn_truncate,
852 .getattr = xfs_vn_getattr,
853 .setattr = xfs_vn_setattr,
854 .setxattr = xfs_vn_setxattr,
855 .getxattr = xfs_vn_getxattr,
856 .listxattr = xfs_vn_listxattr,
857 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858};
859
Nathan Scott416c6d52006-03-14 14:00:51 +1100860struct inode_operations xfs_dir_inode_operations = {
861 .create = xfs_vn_create,
862 .lookup = xfs_vn_lookup,
863 .link = xfs_vn_link,
864 .unlink = xfs_vn_unlink,
865 .symlink = xfs_vn_symlink,
866 .mkdir = xfs_vn_mkdir,
867 .rmdir = xfs_vn_rmdir,
868 .mknod = xfs_vn_mknod,
869 .rename = xfs_vn_rename,
870 .permission = xfs_vn_permission,
871 .getattr = xfs_vn_getattr,
872 .setattr = xfs_vn_setattr,
873 .setxattr = xfs_vn_setxattr,
874 .getxattr = xfs_vn_getxattr,
875 .listxattr = xfs_vn_listxattr,
876 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877};
878
Nathan Scott416c6d52006-03-14 14:00:51 +1100879struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100881 .follow_link = xfs_vn_follow_link,
882 .put_link = xfs_vn_put_link,
883 .permission = xfs_vn_permission,
884 .getattr = xfs_vn_getattr,
885 .setattr = xfs_vn_setattr,
886 .setxattr = xfs_vn_setxattr,
887 .getxattr = xfs_vn_getxattr,
888 .listxattr = xfs_vn_listxattr,
889 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890};