blob: d7f6f2d8ac8ec1c3b8f3185d3e18e958f8dc65a6 [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
201validate_fields(
202 struct inode *ip)
203{
204 vnode_t *vp = LINVFS_GET_VP(ip);
205 vattr_t va;
206 int error;
207
208 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
209 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
210 if (likely(!error)) {
211 ip->i_nlink = va.va_nlink;
212 ip->i_blocks = va.va_nblocks;
213
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800214 /* we're under i_mutex so i_size can't change under us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (i_size_read(ip) != va.va_size)
216 i_size_write(ip, va.va_size);
217 }
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
227linvfs_init_security(
228 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
260has_fs_struct(struct task_struct *task)
261{
262 return (task->fs != init_task.fs);
263}
264
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100265STATIC inline void
266cleanup_inode(
267 vnode_t *dvp,
268 vnode_t *vp,
269 struct dentry *dentry,
270 int mode)
271{
272 struct dentry teardown = {};
273 int err2;
274
275 /* Oh, the horror.
276 * If we can't add the ACL or we fail in
277 * linvfs_init_security we must back out.
278 * 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))
284 VOP_RMDIR(dvp, &teardown, NULL, err2);
285 else
286 VOP_REMOVE(dvp, &teardown, NULL, err2);
287 VN_RELE(vp);
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290STATIC int
291linvfs_mknod(
292 struct inode *dir,
293 struct dentry *dentry,
294 int mode,
295 dev_t rdev)
296{
297 struct inode *ip;
298 vattr_t va;
299 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 */
308 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
309 return -EINVAL;
310
311 if (test_default_acl && test_default_acl(dvp)) {
312 if (!_ACL_ALLOC(default_acl))
313 return -ENOMEM;
314 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
315 _ACL_FREE(default_acl);
316 default_acl = NULL;
317 }
318 }
319
320 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
321 mode &= ~current->fs->umask;
322
323 memset(&va, 0, sizeof(va));
324 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 va.va_mode = mode;
326
327 switch (mode & S_IFMT) {
328 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
329 va.va_rdev = sysv_encode_dev(rdev);
330 va.va_mask |= XFS_AT_RDEV;
331 /*FALLTHROUGH*/
332 case S_IFREG:
333 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
334 break;
335 case S_IFDIR:
336 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
337 break;
338 default:
339 error = EINVAL;
340 break;
341 }
342
Nathan Scott446ada42006-01-11 15:35:44 +1100343 if (!error)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100344 {
Nathan Scott446ada42006-01-11 15:35:44 +1100345 error = linvfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100346 if (error)
347 cleanup_inode(dvp, vp, dentry, mode);
348 }
Nathan Scott446ada42006-01-11 15:35:44 +1100349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (default_acl) {
351 if (!error) {
352 error = _ACL_INHERIT(vp, &va, default_acl);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100353 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 VMODIFY(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100355 else
356 cleanup_inode(dvp, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 _ACL_FREE(default_acl);
359 }
360
361 if (!error) {
362 ASSERT(vp);
363 ip = LINVFS_GET_IP(vp);
364
365 if (S_ISCHR(mode) || S_ISBLK(mode))
366 ip->i_rdev = rdev;
367 else if (S_ISDIR(mode))
368 validate_fields(ip);
369 d_instantiate(dentry, ip);
370 validate_fields(dir);
371 }
372 return -error;
373}
374
375STATIC int
376linvfs_create(
377 struct inode *dir,
378 struct dentry *dentry,
379 int mode,
380 struct nameidata *nd)
381{
382 return linvfs_mknod(dir, dentry, mode, 0);
383}
384
385STATIC int
386linvfs_mkdir(
387 struct inode *dir,
388 struct dentry *dentry,
389 int mode)
390{
391 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
392}
393
394STATIC struct dentry *
395linvfs_lookup(
396 struct inode *dir,
397 struct dentry *dentry,
398 struct nameidata *nd)
399{
400 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
401 int error;
402
403 if (dentry->d_name.len >= MAXNAMELEN)
404 return ERR_PTR(-ENAMETOOLONG);
405
406 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
407 if (error) {
408 if (unlikely(error != ENOENT))
409 return ERR_PTR(-error);
410 d_add(dentry, NULL);
411 return NULL;
412 }
413
414 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
415}
416
417STATIC int
418linvfs_link(
419 struct dentry *old_dentry,
420 struct inode *dir,
421 struct dentry *dentry)
422{
423 struct inode *ip; /* inode of guy being linked to */
424 vnode_t *tdvp; /* target directory for new name/link */
425 vnode_t *vp; /* vp of name being linked */
426 int error;
427
428 ip = old_dentry->d_inode; /* inode being linked to */
429 if (S_ISDIR(ip->i_mode))
430 return -EPERM;
431
432 tdvp = LINVFS_GET_VP(dir);
433 vp = LINVFS_GET_VP(ip);
434
435 VOP_LINK(tdvp, vp, dentry, NULL, error);
436 if (!error) {
437 VMODIFY(tdvp);
438 VN_HOLD(vp);
439 validate_fields(ip);
440 d_instantiate(dentry, ip);
441 }
442 return -error;
443}
444
445STATIC int
446linvfs_unlink(
447 struct inode *dir,
448 struct dentry *dentry)
449{
450 struct inode *inode;
451 vnode_t *dvp; /* directory containing name to remove */
452 int error;
453
454 inode = dentry->d_inode;
455 dvp = LINVFS_GET_VP(dir);
456
457 VOP_REMOVE(dvp, dentry, NULL, error);
458 if (!error) {
459 validate_fields(dir); /* For size only */
460 validate_fields(inode);
461 }
462
463 return -error;
464}
465
466STATIC int
467linvfs_symlink(
468 struct inode *dir,
469 struct dentry *dentry,
470 const char *symname)
471{
472 struct inode *ip;
473 vattr_t va;
474 vnode_t *dvp; /* directory containing name of symlink */
475 vnode_t *cvp; /* used to lookup symlink to put in dentry */
476 int error;
477
478 dvp = LINVFS_GET_VP(dir);
479 cvp = NULL;
480
481 memset(&va, 0, sizeof(va));
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000482 va.va_mode = S_IFLNK |
483 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
485
486 error = 0;
487 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
Nathan Scott54245702006-01-16 15:54:05 +1100488 if (likely(!error && cvp)) {
489 error = linvfs_init_security(cvp, dir);
490 if (likely(!error)) {
491 ip = LINVFS_GET_IP(cvp);
492 d_instantiate(dentry, ip);
493 validate_fields(dir);
494 validate_fields(ip);
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 return -error;
498}
499
500STATIC int
501linvfs_rmdir(
502 struct inode *dir,
503 struct dentry *dentry)
504{
505 struct inode *inode = dentry->d_inode;
506 vnode_t *dvp = LINVFS_GET_VP(dir);
507 int error;
508
509 VOP_RMDIR(dvp, dentry, NULL, error);
510 if (!error) {
511 validate_fields(inode);
512 validate_fields(dir);
513 }
514 return -error;
515}
516
517STATIC int
518linvfs_rename(
519 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 */
527 int error;
528
529 fvp = LINVFS_GET_VP(odir);
530 tvp = LINVFS_GET_VP(ndir);
531
532 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
533 if (error)
534 return -error;
535
536 if (new_inode)
537 validate_fields(new_inode);
538
539 validate_fields(odir);
540 if (ndir != odir)
541 validate_fields(ndir);
542 return 0;
543}
544
545/*
546 * careful here - this function can get called recursively, so
547 * we need to be very careful about how much stack we use.
548 * uio is kmalloced for this reason...
549 */
Al Viro008b1502005-08-20 00:17:39 +0100550STATIC void *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551linvfs_follow_link(
552 struct dentry *dentry,
553 struct nameidata *nd)
554{
555 vnode_t *vp;
556 uio_t *uio;
557 iovec_t iov;
558 int error;
559 char *link;
560
561 ASSERT(dentry);
562 ASSERT(nd);
563
Nathan Scott0d1335b2006-01-12 10:32:51 +1100564 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!link) {
566 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100567 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
571 if (!uio) {
572 kfree(link);
573 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100574 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 vp = LINVFS_GET_VP(dentry->d_inode);
578
579 iov.iov_base = link;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100580 iov.iov_len = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 uio->uio_iov = &iov;
583 uio->uio_offset = 0;
584 uio->uio_segflg = UIO_SYSSPACE;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100585 uio->uio_resid = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 uio->uio_iovcnt = 1;
587
588 VOP_READLINK(vp, uio, 0, NULL, error);
589 if (error) {
590 kfree(link);
591 link = ERR_PTR(-error);
592 } else {
Nathan Scott0d1335b2006-01-12 10:32:51 +1100593 link[MAXPATHLEN - uio->uio_resid] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 kfree(uio);
596
597 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100598 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Nathan Scottcde410a2005-09-05 11:47:01 +1000601STATIC void
602linvfs_put_link(
603 struct dentry *dentry,
604 struct nameidata *nd,
605 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Nathan Scottcde410a2005-09-05 11:47:01 +1000607 char *s = nd_get_link(nd);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!IS_ERR(s))
610 kfree(s);
611}
612
613#ifdef CONFIG_XFS_POSIX_ACL
614STATIC int
615linvfs_permission(
616 struct inode *inode,
617 int mode,
618 struct nameidata *nd)
619{
620 vnode_t *vp = LINVFS_GET_VP(inode);
621 int error;
622
623 mode <<= 6; /* convert from linux to vnode access bits */
624 VOP_ACCESS(vp, mode, NULL, error);
625 return -error;
626}
627#else
628#define linvfs_permission NULL
629#endif
630
631STATIC int
632linvfs_getattr(
633 struct vfsmount *mnt,
634 struct dentry *dentry,
635 struct kstat *stat)
636{
637 struct inode *inode = dentry->d_inode;
638 vnode_t *vp = LINVFS_GET_VP(inode);
639 int error = 0;
640
641 if (unlikely(vp->v_flag & VMODIFIED))
642 error = vn_revalidate(vp);
643 if (!error)
644 generic_fillattr(inode, stat);
645 return 0;
646}
647
648STATIC int
649linvfs_setattr(
650 struct dentry *dentry,
651 struct iattr *attr)
652{
653 struct inode *inode = dentry->d_inode;
654 unsigned int ia_valid = attr->ia_valid;
655 vnode_t *vp = LINVFS_GET_VP(inode);
656 vattr_t vattr;
657 int flags = 0;
658 int error;
659
660 memset(&vattr, 0, sizeof(vattr_t));
661 if (ia_valid & ATTR_UID) {
662 vattr.va_mask |= XFS_AT_UID;
663 vattr.va_uid = attr->ia_uid;
664 }
665 if (ia_valid & ATTR_GID) {
666 vattr.va_mask |= XFS_AT_GID;
667 vattr.va_gid = attr->ia_gid;
668 }
669 if (ia_valid & ATTR_SIZE) {
670 vattr.va_mask |= XFS_AT_SIZE;
671 vattr.va_size = attr->ia_size;
672 }
673 if (ia_valid & ATTR_ATIME) {
674 vattr.va_mask |= XFS_AT_ATIME;
675 vattr.va_atime = attr->ia_atime;
Nathan Scott9bd6f132006-02-07 20:27:44 +1100676 if (ia_valid & ATTR_ATIME_SET)
677 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 if (ia_valid & ATTR_MTIME) {
680 vattr.va_mask |= XFS_AT_MTIME;
681 vattr.va_mtime = attr->ia_mtime;
682 }
683 if (ia_valid & ATTR_CTIME) {
684 vattr.va_mask |= XFS_AT_CTIME;
685 vattr.va_ctime = attr->ia_ctime;
686 }
687 if (ia_valid & ATTR_MODE) {
688 vattr.va_mask |= XFS_AT_MODE;
689 vattr.va_mode = attr->ia_mode;
690 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
691 inode->i_mode &= ~S_ISGID;
692 }
693
694 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
695 flags |= ATTR_UTIME;
696#ifdef ATTR_NO_BLOCK
697 if ((ia_valid & ATTR_NO_BLOCK))
698 flags |= ATTR_NONBLOCK;
699#endif
700
701 VOP_SETATTR(vp, &vattr, flags, NULL, error);
702 if (error)
703 return -error;
704 vn_revalidate(vp);
705 return error;
706}
707
708STATIC void
709linvfs_truncate(
710 struct inode *inode)
711{
712 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
713}
714
715STATIC int
716linvfs_setxattr(
717 struct dentry *dentry,
718 const char *name,
719 const void *data,
720 size_t size,
721 int flags)
722{
723 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
724 char *attr = (char *)name;
725 attrnames_t *namesp;
726 int xflags = 0;
727 int error;
728
729 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
730 if (!namesp)
731 return -EOPNOTSUPP;
732 attr += namesp->attr_namelen;
733 error = namesp->attr_capable(vp, NULL);
734 if (error)
735 return error;
736
737 /* Convert Linux syscall to XFS internal ATTR flags */
738 if (flags & XATTR_CREATE)
739 xflags |= ATTR_CREATE;
740 if (flags & XATTR_REPLACE)
741 xflags |= ATTR_REPLACE;
742 xflags |= namesp->attr_flag;
743 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
744}
745
746STATIC ssize_t
747linvfs_getxattr(
748 struct dentry *dentry,
749 const char *name,
750 void *data,
751 size_t size)
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 ssize_t 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 (!size) {
769 xflags |= ATTR_KERNOVAL;
770 data = NULL;
771 }
772 xflags |= namesp->attr_flag;
773 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
774}
775
776STATIC ssize_t
777linvfs_listxattr(
778 struct dentry *dentry,
779 char *data,
780 size_t size)
781{
782 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
783 int error, xflags = ATTR_KERNAMELS;
784 ssize_t result;
785
786 if (!size)
787 xflags |= ATTR_KERNOVAL;
788 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
789
790 error = attr_generic_list(vp, data, size, xflags, &result);
791 if (error < 0)
792 return error;
793 return result;
794}
795
796STATIC int
797linvfs_removexattr(
798 struct dentry *dentry,
799 const char *name)
800{
801 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
802 char *attr = (char *)name;
803 attrnames_t *namesp;
804 int xflags = 0;
805 int error;
806
807 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
808 if (!namesp)
809 return -EOPNOTSUPP;
810 attr += namesp->attr_namelen;
811 error = namesp->attr_capable(vp, NULL);
812 if (error)
813 return error;
814 xflags |= namesp->attr_flag;
815 return namesp->attr_remove(vp, attr, xflags);
816}
817
818
819struct inode_operations linvfs_file_inode_operations = {
820 .permission = linvfs_permission,
821 .truncate = linvfs_truncate,
822 .getattr = linvfs_getattr,
823 .setattr = linvfs_setattr,
824 .setxattr = linvfs_setxattr,
825 .getxattr = linvfs_getxattr,
826 .listxattr = linvfs_listxattr,
827 .removexattr = linvfs_removexattr,
828};
829
830struct inode_operations linvfs_dir_inode_operations = {
831 .create = linvfs_create,
832 .lookup = linvfs_lookup,
833 .link = linvfs_link,
834 .unlink = linvfs_unlink,
835 .symlink = linvfs_symlink,
836 .mkdir = linvfs_mkdir,
837 .rmdir = linvfs_rmdir,
838 .mknod = linvfs_mknod,
839 .rename = linvfs_rename,
840 .permission = linvfs_permission,
841 .getattr = linvfs_getattr,
842 .setattr = linvfs_setattr,
843 .setxattr = linvfs_setxattr,
844 .getxattr = linvfs_getxattr,
845 .listxattr = linvfs_listxattr,
846 .removexattr = linvfs_removexattr,
847};
848
849struct inode_operations linvfs_symlink_inode_operations = {
850 .readlink = generic_readlink,
851 .follow_link = linvfs_follow_link,
852 .put_link = linvfs_put_link,
853 .permission = linvfs_permission,
854 .getattr = linvfs_getattr,
855 .setattr = linvfs_setattr,
856 .setxattr = linvfs_setxattr,
857 .getxattr = linvfs_getxattr,
858 .listxattr = linvfs_listxattr,
859 .removexattr = linvfs_removexattr,
860};