Pull xpc-disengage into release branch
[deliverable/linux.git] / fs / xfs / linux-2.6 / xfs_iops.c
1 /*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include "xfs.h"
34 #include "xfs_fs.h"
35 #include "xfs_inum.h"
36 #include "xfs_log.h"
37 #include "xfs_trans.h"
38 #include "xfs_sb.h"
39 #include "xfs_ag.h"
40 #include "xfs_dir.h"
41 #include "xfs_dir2.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_bmap.h"
57 #include "xfs_bit.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
61 #include "xfs_rw.h"
62 #include "xfs_acl.h"
63 #include "xfs_cap.h"
64 #include "xfs_mac.h"
65 #include "xfs_attr.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_utils.h"
68
69 #include <linux/xattr.h>
70 #include <linux/namei.h>
71
72
73 /*
74 * Pull the link count and size up from the xfs inode to the linux inode
75 */
76 STATIC void
77 validate_fields(
78 struct inode *ip)
79 {
80 vnode_t *vp = LINVFS_GET_VP(ip);
81 vattr_t va;
82 int error;
83
84 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
85 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
86 if (likely(!error)) {
87 ip->i_nlink = va.va_nlink;
88 ip->i_blocks = va.va_nblocks;
89
90 /* we're under i_sem so i_size can't change under us */
91 if (i_size_read(ip) != va.va_size)
92 i_size_write(ip, va.va_size);
93 }
94 }
95
96 /*
97 * Determine whether a process has a valid fs_struct (kernel daemons
98 * like knfsd don't have an fs_struct).
99 *
100 * XXX(hch): nfsd is broken, better fix it instead.
101 */
102 STATIC inline int
103 has_fs_struct(struct task_struct *task)
104 {
105 return (task->fs != init_task.fs);
106 }
107
108 STATIC int
109 linvfs_mknod(
110 struct inode *dir,
111 struct dentry *dentry,
112 int mode,
113 dev_t rdev)
114 {
115 struct inode *ip;
116 vattr_t va;
117 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
118 xfs_acl_t *default_acl = NULL;
119 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
120 int error;
121
122 /*
123 * Irix uses Missed'em'V split, but doesn't want to see
124 * the upper 5 bits of (14bit) major.
125 */
126 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
127 return -EINVAL;
128
129 if (test_default_acl && test_default_acl(dvp)) {
130 if (!_ACL_ALLOC(default_acl))
131 return -ENOMEM;
132 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
133 _ACL_FREE(default_acl);
134 default_acl = NULL;
135 }
136 }
137
138 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
139 mode &= ~current->fs->umask;
140
141 memset(&va, 0, sizeof(va));
142 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
143 va.va_mode = mode;
144
145 switch (mode & S_IFMT) {
146 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
147 va.va_rdev = sysv_encode_dev(rdev);
148 va.va_mask |= XFS_AT_RDEV;
149 /*FALLTHROUGH*/
150 case S_IFREG:
151 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
152 break;
153 case S_IFDIR:
154 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
155 break;
156 default:
157 error = EINVAL;
158 break;
159 }
160
161 if (default_acl) {
162 if (!error) {
163 error = _ACL_INHERIT(vp, &va, default_acl);
164 if (!error) {
165 VMODIFY(vp);
166 } else {
167 struct dentry teardown = {};
168 int err2;
169
170 /* Oh, the horror.
171 * If we can't add the ACL we must back out.
172 * ENOSPC can hit here, among other things.
173 */
174 teardown.d_inode = ip = LINVFS_GET_IP(vp);
175 teardown.d_name = dentry->d_name;
176
177 vn_mark_bad(vp);
178
179 if (S_ISDIR(mode))
180 VOP_RMDIR(dvp, &teardown, NULL, err2);
181 else
182 VOP_REMOVE(dvp, &teardown, NULL, err2);
183 VN_RELE(vp);
184 }
185 }
186 _ACL_FREE(default_acl);
187 }
188
189 if (!error) {
190 ASSERT(vp);
191 ip = LINVFS_GET_IP(vp);
192
193 if (S_ISCHR(mode) || S_ISBLK(mode))
194 ip->i_rdev = rdev;
195 else if (S_ISDIR(mode))
196 validate_fields(ip);
197 d_instantiate(dentry, ip);
198 validate_fields(dir);
199 }
200 return -error;
201 }
202
203 STATIC int
204 linvfs_create(
205 struct inode *dir,
206 struct dentry *dentry,
207 int mode,
208 struct nameidata *nd)
209 {
210 return linvfs_mknod(dir, dentry, mode, 0);
211 }
212
213 STATIC int
214 linvfs_mkdir(
215 struct inode *dir,
216 struct dentry *dentry,
217 int mode)
218 {
219 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
220 }
221
222 STATIC struct dentry *
223 linvfs_lookup(
224 struct inode *dir,
225 struct dentry *dentry,
226 struct nameidata *nd)
227 {
228 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
229 int error;
230
231 if (dentry->d_name.len >= MAXNAMELEN)
232 return ERR_PTR(-ENAMETOOLONG);
233
234 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
235 if (error) {
236 if (unlikely(error != ENOENT))
237 return ERR_PTR(-error);
238 d_add(dentry, NULL);
239 return NULL;
240 }
241
242 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
243 }
244
245 STATIC int
246 linvfs_link(
247 struct dentry *old_dentry,
248 struct inode *dir,
249 struct dentry *dentry)
250 {
251 struct inode *ip; /* inode of guy being linked to */
252 vnode_t *tdvp; /* target directory for new name/link */
253 vnode_t *vp; /* vp of name being linked */
254 int error;
255
256 ip = old_dentry->d_inode; /* inode being linked to */
257 if (S_ISDIR(ip->i_mode))
258 return -EPERM;
259
260 tdvp = LINVFS_GET_VP(dir);
261 vp = LINVFS_GET_VP(ip);
262
263 VOP_LINK(tdvp, vp, dentry, NULL, error);
264 if (!error) {
265 VMODIFY(tdvp);
266 VN_HOLD(vp);
267 validate_fields(ip);
268 d_instantiate(dentry, ip);
269 }
270 return -error;
271 }
272
273 STATIC int
274 linvfs_unlink(
275 struct inode *dir,
276 struct dentry *dentry)
277 {
278 struct inode *inode;
279 vnode_t *dvp; /* directory containing name to remove */
280 int error;
281
282 inode = dentry->d_inode;
283 dvp = LINVFS_GET_VP(dir);
284
285 VOP_REMOVE(dvp, dentry, NULL, error);
286 if (!error) {
287 validate_fields(dir); /* For size only */
288 validate_fields(inode);
289 }
290
291 return -error;
292 }
293
294 STATIC int
295 linvfs_symlink(
296 struct inode *dir,
297 struct dentry *dentry,
298 const char *symname)
299 {
300 struct inode *ip;
301 vattr_t va;
302 vnode_t *dvp; /* directory containing name of symlink */
303 vnode_t *cvp; /* used to lookup symlink to put in dentry */
304 int error;
305
306 dvp = LINVFS_GET_VP(dir);
307 cvp = NULL;
308
309 memset(&va, 0, sizeof(va));
310 va.va_mode = S_IFLNK |
311 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
312 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
313
314 error = 0;
315 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
316 if (!error && cvp) {
317 ip = LINVFS_GET_IP(cvp);
318 d_instantiate(dentry, ip);
319 validate_fields(dir);
320 validate_fields(ip); /* size needs update */
321 }
322 return -error;
323 }
324
325 STATIC int
326 linvfs_rmdir(
327 struct inode *dir,
328 struct dentry *dentry)
329 {
330 struct inode *inode = dentry->d_inode;
331 vnode_t *dvp = LINVFS_GET_VP(dir);
332 int error;
333
334 VOP_RMDIR(dvp, dentry, NULL, error);
335 if (!error) {
336 validate_fields(inode);
337 validate_fields(dir);
338 }
339 return -error;
340 }
341
342 STATIC int
343 linvfs_rename(
344 struct inode *odir,
345 struct dentry *odentry,
346 struct inode *ndir,
347 struct dentry *ndentry)
348 {
349 struct inode *new_inode = ndentry->d_inode;
350 vnode_t *fvp; /* from directory */
351 vnode_t *tvp; /* target directory */
352 int error;
353
354 fvp = LINVFS_GET_VP(odir);
355 tvp = LINVFS_GET_VP(ndir);
356
357 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
358 if (error)
359 return -error;
360
361 if (new_inode)
362 validate_fields(new_inode);
363
364 validate_fields(odir);
365 if (ndir != odir)
366 validate_fields(ndir);
367 return 0;
368 }
369
370 /*
371 * careful here - this function can get called recursively, so
372 * we need to be very careful about how much stack we use.
373 * uio is kmalloced for this reason...
374 */
375 STATIC void *
376 linvfs_follow_link(
377 struct dentry *dentry,
378 struct nameidata *nd)
379 {
380 vnode_t *vp;
381 uio_t *uio;
382 iovec_t iov;
383 int error;
384 char *link;
385
386 ASSERT(dentry);
387 ASSERT(nd);
388
389 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
390 if (!link) {
391 nd_set_link(nd, ERR_PTR(-ENOMEM));
392 return NULL;
393 }
394
395 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
396 if (!uio) {
397 kfree(link);
398 nd_set_link(nd, ERR_PTR(-ENOMEM));
399 return NULL;
400 }
401
402 vp = LINVFS_GET_VP(dentry->d_inode);
403
404 iov.iov_base = link;
405 iov.iov_len = MAXNAMELEN;
406
407 uio->uio_iov = &iov;
408 uio->uio_offset = 0;
409 uio->uio_segflg = UIO_SYSSPACE;
410 uio->uio_resid = MAXNAMELEN;
411 uio->uio_iovcnt = 1;
412
413 VOP_READLINK(vp, uio, 0, NULL, error);
414 if (error) {
415 kfree(link);
416 link = ERR_PTR(-error);
417 } else {
418 link[MAXNAMELEN - uio->uio_resid] = '\0';
419 }
420 kfree(uio);
421
422 nd_set_link(nd, link);
423 return NULL;
424 }
425
426 STATIC void
427 linvfs_put_link(
428 struct dentry *dentry,
429 struct nameidata *nd,
430 void *p)
431 {
432 char *s = nd_get_link(nd);
433
434 if (!IS_ERR(s))
435 kfree(s);
436 }
437
438 #ifdef CONFIG_XFS_POSIX_ACL
439 STATIC int
440 linvfs_permission(
441 struct inode *inode,
442 int mode,
443 struct nameidata *nd)
444 {
445 vnode_t *vp = LINVFS_GET_VP(inode);
446 int error;
447
448 mode <<= 6; /* convert from linux to vnode access bits */
449 VOP_ACCESS(vp, mode, NULL, error);
450 return -error;
451 }
452 #else
453 #define linvfs_permission NULL
454 #endif
455
456 STATIC int
457 linvfs_getattr(
458 struct vfsmount *mnt,
459 struct dentry *dentry,
460 struct kstat *stat)
461 {
462 struct inode *inode = dentry->d_inode;
463 vnode_t *vp = LINVFS_GET_VP(inode);
464 int error = 0;
465
466 if (unlikely(vp->v_flag & VMODIFIED))
467 error = vn_revalidate(vp);
468 if (!error)
469 generic_fillattr(inode, stat);
470 return 0;
471 }
472
473 STATIC int
474 linvfs_setattr(
475 struct dentry *dentry,
476 struct iattr *attr)
477 {
478 struct inode *inode = dentry->d_inode;
479 unsigned int ia_valid = attr->ia_valid;
480 vnode_t *vp = LINVFS_GET_VP(inode);
481 vattr_t vattr;
482 int flags = 0;
483 int error;
484
485 memset(&vattr, 0, sizeof(vattr_t));
486 if (ia_valid & ATTR_UID) {
487 vattr.va_mask |= XFS_AT_UID;
488 vattr.va_uid = attr->ia_uid;
489 }
490 if (ia_valid & ATTR_GID) {
491 vattr.va_mask |= XFS_AT_GID;
492 vattr.va_gid = attr->ia_gid;
493 }
494 if (ia_valid & ATTR_SIZE) {
495 vattr.va_mask |= XFS_AT_SIZE;
496 vattr.va_size = attr->ia_size;
497 }
498 if (ia_valid & ATTR_ATIME) {
499 vattr.va_mask |= XFS_AT_ATIME;
500 vattr.va_atime = attr->ia_atime;
501 }
502 if (ia_valid & ATTR_MTIME) {
503 vattr.va_mask |= XFS_AT_MTIME;
504 vattr.va_mtime = attr->ia_mtime;
505 }
506 if (ia_valid & ATTR_CTIME) {
507 vattr.va_mask |= XFS_AT_CTIME;
508 vattr.va_ctime = attr->ia_ctime;
509 }
510 if (ia_valid & ATTR_MODE) {
511 vattr.va_mask |= XFS_AT_MODE;
512 vattr.va_mode = attr->ia_mode;
513 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
514 inode->i_mode &= ~S_ISGID;
515 }
516
517 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
518 flags |= ATTR_UTIME;
519 #ifdef ATTR_NO_BLOCK
520 if ((ia_valid & ATTR_NO_BLOCK))
521 flags |= ATTR_NONBLOCK;
522 #endif
523
524 VOP_SETATTR(vp, &vattr, flags, NULL, error);
525 if (error)
526 return -error;
527 vn_revalidate(vp);
528 return error;
529 }
530
531 STATIC void
532 linvfs_truncate(
533 struct inode *inode)
534 {
535 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
536 }
537
538 STATIC int
539 linvfs_setxattr(
540 struct dentry *dentry,
541 const char *name,
542 const void *data,
543 size_t size,
544 int flags)
545 {
546 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
547 char *attr = (char *)name;
548 attrnames_t *namesp;
549 int xflags = 0;
550 int error;
551
552 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
553 if (!namesp)
554 return -EOPNOTSUPP;
555 attr += namesp->attr_namelen;
556 error = namesp->attr_capable(vp, NULL);
557 if (error)
558 return error;
559
560 /* Convert Linux syscall to XFS internal ATTR flags */
561 if (flags & XATTR_CREATE)
562 xflags |= ATTR_CREATE;
563 if (flags & XATTR_REPLACE)
564 xflags |= ATTR_REPLACE;
565 xflags |= namesp->attr_flag;
566 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
567 }
568
569 STATIC ssize_t
570 linvfs_getxattr(
571 struct dentry *dentry,
572 const char *name,
573 void *data,
574 size_t size)
575 {
576 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
577 char *attr = (char *)name;
578 attrnames_t *namesp;
579 int xflags = 0;
580 ssize_t error;
581
582 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
583 if (!namesp)
584 return -EOPNOTSUPP;
585 attr += namesp->attr_namelen;
586 error = namesp->attr_capable(vp, NULL);
587 if (error)
588 return error;
589
590 /* Convert Linux syscall to XFS internal ATTR flags */
591 if (!size) {
592 xflags |= ATTR_KERNOVAL;
593 data = NULL;
594 }
595 xflags |= namesp->attr_flag;
596 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
597 }
598
599 STATIC ssize_t
600 linvfs_listxattr(
601 struct dentry *dentry,
602 char *data,
603 size_t size)
604 {
605 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
606 int error, xflags = ATTR_KERNAMELS;
607 ssize_t result;
608
609 if (!size)
610 xflags |= ATTR_KERNOVAL;
611 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
612
613 error = attr_generic_list(vp, data, size, xflags, &result);
614 if (error < 0)
615 return error;
616 return result;
617 }
618
619 STATIC int
620 linvfs_removexattr(
621 struct dentry *dentry,
622 const char *name)
623 {
624 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
625 char *attr = (char *)name;
626 attrnames_t *namesp;
627 int xflags = 0;
628 int error;
629
630 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
631 if (!namesp)
632 return -EOPNOTSUPP;
633 attr += namesp->attr_namelen;
634 error = namesp->attr_capable(vp, NULL);
635 if (error)
636 return error;
637 xflags |= namesp->attr_flag;
638 return namesp->attr_remove(vp, attr, xflags);
639 }
640
641
642 struct inode_operations linvfs_file_inode_operations = {
643 .permission = linvfs_permission,
644 .truncate = linvfs_truncate,
645 .getattr = linvfs_getattr,
646 .setattr = linvfs_setattr,
647 .setxattr = linvfs_setxattr,
648 .getxattr = linvfs_getxattr,
649 .listxattr = linvfs_listxattr,
650 .removexattr = linvfs_removexattr,
651 };
652
653 struct inode_operations linvfs_dir_inode_operations = {
654 .create = linvfs_create,
655 .lookup = linvfs_lookup,
656 .link = linvfs_link,
657 .unlink = linvfs_unlink,
658 .symlink = linvfs_symlink,
659 .mkdir = linvfs_mkdir,
660 .rmdir = linvfs_rmdir,
661 .mknod = linvfs_mknod,
662 .rename = linvfs_rename,
663 .permission = linvfs_permission,
664 .getattr = linvfs_getattr,
665 .setattr = linvfs_setattr,
666 .setxattr = linvfs_setxattr,
667 .getxattr = linvfs_getxattr,
668 .listxattr = linvfs_listxattr,
669 .removexattr = linvfs_removexattr,
670 };
671
672 struct inode_operations linvfs_symlink_inode_operations = {
673 .readlink = generic_readlink,
674 .follow_link = linvfs_follow_link,
675 .put_link = linvfs_put_link,
676 .permission = linvfs_permission,
677 .getattr = linvfs_getattr,
678 .setattr = linvfs_setattr,
679 .setxattr = linvfs_setxattr,
680 .getxattr = linvfs_getxattr,
681 .listxattr = linvfs_listxattr,
682 .removexattr = linvfs_removexattr,
683 };
This page took 0.063627 seconds and 6 git commands to generate.