[XFS] cleanup vnode use in dmapi calls
[deliverable/linux.git] / fs / xfs / linux-2.6 / xfs_iops.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4
LT
18#include "xfs.h"
19#include "xfs_fs.h"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
1da177e4 31#include "xfs_bmap_btree.h"
a844f451 32#include "xfs_alloc_btree.h"
1da177e4 33#include "xfs_ialloc_btree.h"
1da177e4 34#include "xfs_dir2_sf.h"
a844f451 35#include "xfs_attr_sf.h"
1da177e4
LT
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
a844f451
NS
39#include "xfs_btree.h"
40#include "xfs_ialloc.h"
1da177e4
LT
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
1da177e4
LT
46#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_utils.h"
739bfb2a 49#include "xfs_vnodeops.h"
1da177e4 50
16f7e0fe 51#include <linux/capability.h>
1da177e4
LT
52#include <linux/xattr.h>
53#include <linux/namei.h>
446ada4a 54#include <linux/security.h>
3ed65264 55#include <linux/falloc.h>
1da177e4 56
42fe2b1f
CH
57/*
58 * Bring the atime in the XFS inode uptodate.
59 * Used before logging the inode to disk or when the Linux inode goes away.
60 */
61void
62xfs_synchronize_atime(
63 xfs_inode_t *ip)
64{
67fcaa73 65 bhv_vnode_t *vp;
42fe2b1f
CH
66
67 vp = XFS_ITOV_NULL(ip);
68 if (vp) {
0a74cd19
CH
69 ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
70 ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
42fe2b1f
CH
71 }
72}
73
5d51eff4
DC
74/*
75 * If the linux inode exists, mark it dirty.
76 * Used when commiting a dirty inode into a transaction so that
77 * the inode will get written back by the linux code
78 */
79void
80xfs_mark_inode_dirty_sync(
81 xfs_inode_t *ip)
82{
83 bhv_vnode_t *vp;
84
85 vp = XFS_ITOV_NULL(ip);
86 if (vp)
87 mark_inode_dirty_sync(vn_to_inode(vp));
88}
89
4aeb664c
NS
90/*
91 * Change the requested timestamp in the given inode.
92 * We don't lock across timestamp updates, and we don't log them but
93 * we do record the fact that there is dirty information in core.
94 *
95 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
96 * with XFS_ICHGTIME_ACC to be sure that access time
97 * update will take. Calling first with XFS_ICHGTIME_ACC
98 * and then XFS_ICHGTIME_MOD may fail to modify the access
99 * timestamp if the filesystem is mounted noacctm.
100 */
101void
102xfs_ichgtime(
103 xfs_inode_t *ip,
104 int flags)
105{
ec86dc02 106 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
4aeb664c
NS
107 timespec_t tv;
108
4aeb664c
NS
109 nanotime(&tv);
110 if (flags & XFS_ICHGTIME_MOD) {
111 inode->i_mtime = tv;
112 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
113 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
114 }
115 if (flags & XFS_ICHGTIME_ACC) {
116 inode->i_atime = tv;
117 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
118 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
119 }
120 if (flags & XFS_ICHGTIME_CHG) {
121 inode->i_ctime = tv;
122 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
123 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
124 }
125
126 /*
127 * We update the i_update_core field _after_ changing
128 * the timestamps in order to coordinate properly with
129 * xfs_iflush() so that we don't lose timestamp updates.
130 * This keeps us from having to hold the inode lock
131 * while doing this. We use the SYNCHRONIZE macro to
132 * ensure that the compiler does not reorder the update
133 * of i_update_core above the timestamp updates above.
134 */
135 SYNCHRONIZE();
136 ip->i_update_core = 1;
cf10e82b 137 if (!(inode->i_state & I_NEW))
4aeb664c
NS
138 mark_inode_dirty_sync(inode);
139}
140
141/*
142 * Variant on the above which avoids querying the system clock
143 * in situations where we know the Linux inode timestamps have
144 * just been updated (and so we can update our inode cheaply).
4aeb664c
NS
145 */
146void
147xfs_ichgtime_fast(
148 xfs_inode_t *ip,
149 struct inode *inode,
150 int flags)
151{
152 timespec_t *tvp;
153
154 /*
42fe2b1f
CH
155 * Atime updates for read() & friends are handled lazily now, and
156 * explicit updates must go through xfs_ichgtime()
4aeb664c 157 */
42fe2b1f 158 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
4aeb664c
NS
159
160 /*
42fe2b1f
CH
161 * We're not supposed to change timestamps in readonly-mounted
162 * filesystems. Throw it away if anyone asks us.
4aeb664c 163 */
42fe2b1f 164 if (unlikely(IS_RDONLY(inode)))
4aeb664c
NS
165 return;
166
167 if (flags & XFS_ICHGTIME_MOD) {
168 tvp = &inode->i_mtime;
169 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
170 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
171 }
4aeb664c
NS
172 if (flags & XFS_ICHGTIME_CHG) {
173 tvp = &inode->i_ctime;
174 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
175 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
176 }
177
178 /*
179 * We update the i_update_core field _after_ changing
180 * the timestamps in order to coordinate properly with
181 * xfs_iflush() so that we don't lose timestamp updates.
182 * This keeps us from having to hold the inode lock
183 * while doing this. We use the SYNCHRONIZE macro to
184 * ensure that the compiler does not reorder the update
185 * of i_update_core above the timestamp updates above.
186 */
187 SYNCHRONIZE();
188 ip->i_update_core = 1;
cf10e82b 189 if (!(inode->i_state & I_NEW))
4aeb664c
NS
190 mark_inode_dirty_sync(inode);
191}
192
1da177e4
LT
193
194/*
195 * Pull the link count and size up from the xfs inode to the linux inode
196 */
197STATIC void
416c6d5b 198xfs_validate_fields(
6572bc28 199 struct inode *inode)
1da177e4 200{
6572bc28
CH
201 struct xfs_inode *ip = XFS_I(inode);
202 loff_t size;
203
6572bc28
CH
204 /* we're under i_sem so i_size can't change under us */
205 size = XFS_ISIZE(ip);
206 if (i_size_read(inode) != size)
207 i_size_write(inode, size);
1da177e4
LT
208}
209
446ada4a
NS
210/*
211 * Hook in SELinux. This is not quite correct yet, what we really need
212 * here (as we do for default ACLs) is a mechanism by which creation of
213 * these attrs can be journalled at inode creation time (along with the
214 * inode, of course, such that log replay can't cause these to be lost).
215 */
216STATIC int
416c6d5b 217xfs_init_security(
67fcaa73 218 bhv_vnode_t *vp,
446ada4a
NS
219 struct inode *dir)
220{
ec86dc02 221 struct inode *ip = vn_to_inode(vp);
446ada4a
NS
222 size_t length;
223 void *value;
224 char *name;
225 int error;
226
227 error = security_inode_init_security(ip, dir, &name, &value, &length);
228 if (error) {
229 if (error == -EOPNOTSUPP)
230 return 0;
231 return -error;
232 }
233
739bfb2a
CH
234 error = xfs_attr_set(XFS_I(ip), name, value,
235 length, ATTR_SECURE);
446ada4a 236 if (!error)
b3aea4ed 237 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
446ada4a
NS
238
239 kfree(name);
240 kfree(value);
241 return error;
242}
243
7989cb8e 244STATIC void
416c6d5b 245xfs_cleanup_inode(
739bfb2a 246 struct inode *dir,
67fcaa73 247 bhv_vnode_t *vp,
220b5284 248 struct dentry *dentry,
3a69c7dc
YL
249 int mode)
250{
251 struct dentry teardown = {};
3a69c7dc
YL
252
253 /* Oh, the horror.
220b5284 254 * If we can't add the ACL or we fail in
416c6d5b 255 * xfs_init_security we must back out.
3a69c7dc
YL
256 * ENOSPC can hit here, among other things.
257 */
ec86dc02 258 teardown.d_inode = vn_to_inode(vp);
3a69c7dc
YL
259 teardown.d_name = dentry->d_name;
260
261 if (S_ISDIR(mode))
739bfb2a 262 xfs_rmdir(XFS_I(dir), &teardown);
3a69c7dc 263 else
739bfb2a 264 xfs_remove(XFS_I(dir), &teardown);
3a69c7dc
YL
265 VN_RELE(vp);
266}
267
1da177e4 268STATIC int
416c6d5b 269xfs_vn_mknod(
1da177e4
LT
270 struct inode *dir,
271 struct dentry *dentry,
272 int mode,
273 dev_t rdev)
274{
db0bb7ba 275 struct inode *inode;
67fcaa73 276 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
1da177e4
LT
277 xfs_acl_t *default_acl = NULL;
278 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
279 int error;
280
281 /*
282 * Irix uses Missed'em'V split, but doesn't want to see
283 * the upper 5 bits of (14bit) major.
284 */
220b5284 285 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
1da177e4
LT
286 return -EINVAL;
287
db0bb7ba 288 if (test_default_acl && test_default_acl(dvp)) {
220b5284 289 if (!_ACL_ALLOC(default_acl)) {
1da177e4 290 return -ENOMEM;
220b5284 291 }
1da177e4
LT
292 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
293 _ACL_FREE(default_acl);
294 default_acl = NULL;
295 }
296 }
297
db0bb7ba 298 if (IS_POSIXACL(dir) && !default_acl)
1da177e4
LT
299 mode &= ~current->fs->umask;
300
1da177e4 301 switch (mode & S_IFMT) {
db0bb7ba
CH
302 case S_IFCHR:
303 case S_IFBLK:
304 case S_IFIFO:
305 case S_IFSOCK:
3e5daf05 306 rdev = sysv_encode_dev(rdev);
1da177e4 307 case S_IFREG:
3e5daf05 308 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
1da177e4
LT
309 break;
310 case S_IFDIR:
3e5daf05 311 error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
1da177e4
LT
312 break;
313 default:
314 error = EINVAL;
315 break;
316 }
317
db0bb7ba
CH
318 if (unlikely(error))
319 goto out_free_acl;
446ada4a 320
db0bb7ba
CH
321 error = xfs_init_security(vp, dir);
322 if (unlikely(error))
323 goto out_cleanup_inode;
324
325 if (default_acl) {
326 error = _ACL_INHERIT(vp, mode, default_acl);
327 if (unlikely(error))
328 goto out_cleanup_inode;
329 xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
1da177e4
LT
330 _ACL_FREE(default_acl);
331 }
332
db0bb7ba 333 inode = vn_to_inode(vp);
1da177e4 334
db0bb7ba
CH
335 if (S_ISDIR(mode))
336 xfs_validate_fields(inode);
337 d_instantiate(dentry, inode);
338 xfs_validate_fields(dir);
339 return -error;
340
341 out_cleanup_inode:
342 xfs_cleanup_inode(dir, vp, dentry, mode);
343 out_free_acl:
344 if (default_acl)
345 _ACL_FREE(default_acl);
1da177e4
LT
346 return -error;
347}
348
349STATIC int
416c6d5b 350xfs_vn_create(
1da177e4
LT
351 struct inode *dir,
352 struct dentry *dentry,
353 int mode,
354 struct nameidata *nd)
355{
416c6d5b 356 return xfs_vn_mknod(dir, dentry, mode, 0);
1da177e4
LT
357}
358
359STATIC int
416c6d5b 360xfs_vn_mkdir(
1da177e4
LT
361 struct inode *dir,
362 struct dentry *dentry,
363 int mode)
364{
416c6d5b 365 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
1da177e4
LT
366}
367
368STATIC struct dentry *
416c6d5b 369xfs_vn_lookup(
1da177e4
LT
370 struct inode *dir,
371 struct dentry *dentry,
372 struct nameidata *nd)
373{
739bfb2a 374 bhv_vnode_t *cvp;
1da177e4
LT
375 int error;
376
377 if (dentry->d_name.len >= MAXNAMELEN)
378 return ERR_PTR(-ENAMETOOLONG);
379
739bfb2a 380 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
67fcaa73 381 if (unlikely(error)) {
1da177e4
LT
382 if (unlikely(error != ENOENT))
383 return ERR_PTR(-error);
384 d_add(dentry, NULL);
385 return NULL;
386 }
387
ec86dc02 388 return d_splice_alias(vn_to_inode(cvp), dentry);
1da177e4
LT
389}
390
391STATIC int
416c6d5b 392xfs_vn_link(
1da177e4
LT
393 struct dentry *old_dentry,
394 struct inode *dir,
395 struct dentry *dentry)
396{
397 struct inode *ip; /* inode of guy being linked to */
67fcaa73 398 bhv_vnode_t *vp; /* vp of name being linked */
1da177e4
LT
399 int error;
400
401 ip = old_dentry->d_inode; /* inode being linked to */
ec86dc02 402 vp = vn_from_inode(ip);
1da177e4 403
97dfd70c 404 VN_HOLD(vp);
739bfb2a 405 error = xfs_link(XFS_I(dir), vp, dentry);
97dfd70c
NS
406 if (unlikely(error)) {
407 VN_RELE(vp);
408 } else {
b3aea4ed 409 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
6572bc28 410 xfs_validate_fields(ip);
1da177e4
LT
411 d_instantiate(dentry, ip);
412 }
413 return -error;
414}
415
416STATIC int
416c6d5b 417xfs_vn_unlink(
1da177e4
LT
418 struct inode *dir,
419 struct dentry *dentry)
420{
421 struct inode *inode;
1da177e4
LT
422 int error;
423
424 inode = dentry->d_inode;
1da177e4 425
739bfb2a 426 error = xfs_remove(XFS_I(dir), dentry);
220b5284 427 if (likely(!error)) {
6572bc28
CH
428 xfs_validate_fields(dir); /* size needs update */
429 xfs_validate_fields(inode);
1da177e4 430 }
1da177e4
LT
431 return -error;
432}
433
434STATIC int
416c6d5b 435xfs_vn_symlink(
1da177e4
LT
436 struct inode *dir,
437 struct dentry *dentry,
438 const char *symname)
439{
440 struct inode *ip;
67fcaa73 441 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
1da177e4 442 int error;
3e5daf05 443 mode_t mode;
1da177e4 444
1da177e4
LT
445 cvp = NULL;
446
3e5daf05 447 mode = S_IFLNK |
0432dab2 448 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
1da177e4 449
3e5daf05
CH
450 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
451 &cvp, NULL);
54245702 452 if (likely(!error && cvp)) {
416c6d5b 453 error = xfs_init_security(cvp, dir);
54245702 454 if (likely(!error)) {
ec86dc02 455 ip = vn_to_inode(cvp);
54245702 456 d_instantiate(dentry, ip);
6572bc28
CH
457 xfs_validate_fields(dir);
458 xfs_validate_fields(ip);
ce9d37c2 459 } else {
739bfb2a 460 xfs_cleanup_inode(dir, cvp, dentry, 0);
54245702 461 }
1da177e4
LT
462 }
463 return -error;
464}
465
466STATIC int
416c6d5b 467xfs_vn_rmdir(
1da177e4
LT
468 struct inode *dir,
469 struct dentry *dentry)
470{
471 struct inode *inode = dentry->d_inode;
1da177e4
LT
472 int error;
473
739bfb2a 474 error = xfs_rmdir(XFS_I(dir), dentry);
220b5284 475 if (likely(!error)) {
6572bc28
CH
476 xfs_validate_fields(inode);
477 xfs_validate_fields(dir);
1da177e4
LT
478 }
479 return -error;
480}
481
482STATIC int
416c6d5b 483xfs_vn_rename(
1da177e4
LT
484 struct inode *odir,
485 struct dentry *odentry,
486 struct inode *ndir,
487 struct dentry *ndentry)
488{
489 struct inode *new_inode = ndentry->d_inode;
67fcaa73 490 bhv_vnode_t *tvp; /* target directory */
1da177e4
LT
491 int error;
492
ec86dc02 493 tvp = vn_from_inode(ndir);
1da177e4 494
739bfb2a 495 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
220b5284
NS
496 if (likely(!error)) {
497 if (new_inode)
6572bc28
CH
498 xfs_validate_fields(new_inode);
499 xfs_validate_fields(odir);
220b5284 500 if (ndir != odir)
6572bc28 501 xfs_validate_fields(ndir);
220b5284 502 }
220b5284 503 return -error;
1da177e4
LT
504}
505
506/*
507 * careful here - this function can get called recursively, so
508 * we need to be very careful about how much stack we use.
509 * uio is kmalloced for this reason...
510 */
008b150a 511STATIC void *
416c6d5b 512xfs_vn_follow_link(
1da177e4
LT
513 struct dentry *dentry,
514 struct nameidata *nd)
515{
1da177e4 516 char *link;
804c83c3 517 int error = -ENOMEM;
1da177e4 518
f52720ca 519 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
804c83c3
CH
520 if (!link)
521 goto out_err;
1da177e4 522
739bfb2a 523 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
804c83c3
CH
524 if (unlikely(error))
525 goto out_kfree;
1da177e4
LT
526
527 nd_set_link(nd, link);
008b150a 528 return NULL;
804c83c3
CH
529
530 out_kfree:
531 kfree(link);
532 out_err:
533 nd_set_link(nd, ERR_PTR(error));
534 return NULL;
1da177e4
LT
535}
536
cde410a9 537STATIC void
416c6d5b 538xfs_vn_put_link(
cde410a9
NS
539 struct dentry *dentry,
540 struct nameidata *nd,
541 void *p)
1da177e4 542{
cde410a9
NS
543 char *s = nd_get_link(nd);
544
1da177e4
LT
545 if (!IS_ERR(s))
546 kfree(s);
547}
548
549#ifdef CONFIG_XFS_POSIX_ACL
4576758d
CH
550STATIC int
551xfs_check_acl(
552 struct inode *inode,
553 int mask)
554{
555 struct xfs_inode *ip = XFS_I(inode);
556 int error;
557
558 xfs_itrace_entry(ip);
559
560 if (XFS_IFORK_Q(ip)) {
561 error = xfs_acl_iaccess(ip, mask, NULL);
562 if (error != -1)
563 return -error;
564 }
565
566 return -EAGAIN;
567}
568
1da177e4 569STATIC int
416c6d5b 570xfs_vn_permission(
4576758d
CH
571 struct inode *inode,
572 int mask,
573 struct nameidata *nd)
1da177e4 574{
4576758d 575 return generic_permission(inode, mask, xfs_check_acl);
1da177e4
LT
576}
577#else
416c6d5b 578#define xfs_vn_permission NULL
1da177e4
LT
579#endif
580
581STATIC int
416c6d5b 582xfs_vn_getattr(
c43f4087
CH
583 struct vfsmount *mnt,
584 struct dentry *dentry,
585 struct kstat *stat)
1da177e4 586{
c43f4087
CH
587 struct inode *inode = dentry->d_inode;
588 struct xfs_inode *ip = XFS_I(inode);
589 struct xfs_mount *mp = ip->i_mount;
590
591 xfs_itrace_entry(ip);
592
593 if (XFS_FORCED_SHUTDOWN(mp))
594 return XFS_ERROR(EIO);
595
596 stat->size = XFS_ISIZE(ip);
597 stat->dev = inode->i_sb->s_dev;
598 stat->mode = ip->i_d.di_mode;
599 stat->nlink = ip->i_d.di_nlink;
600 stat->uid = ip->i_d.di_uid;
601 stat->gid = ip->i_d.di_gid;
602 stat->ino = ip->i_ino;
603#if XFS_BIG_INUMS
604 stat->ino += mp->m_inoadd;
605#endif
606 stat->atime = inode->i_atime;
607 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
608 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
609 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
610 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
611 stat->blocks =
612 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
613
614
615 switch (inode->i_mode & S_IFMT) {
616 case S_IFBLK:
617 case S_IFCHR:
618 stat->blksize = BLKDEV_IOSIZE;
619 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
620 sysv_minor(ip->i_df.if_u2.if_rdev));
621 break;
622 default:
71ddabb9 623 if (XFS_IS_REALTIME_INODE(ip)) {
c43f4087
CH
624 /*
625 * If the file blocks are being allocated from a
626 * realtime volume, then return the inode's realtime
627 * extent size or the realtime volume's extent size.
628 */
629 stat->blksize =
630 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
631 } else
632 stat->blksize = xfs_preferred_iosize(mp);
633 stat->rdev = 0;
634 break;
69e23b9a 635 }
c43f4087
CH
636
637 return 0;
1da177e4
LT
638}
639
640STATIC int
416c6d5b 641xfs_vn_setattr(
1da177e4
LT
642 struct dentry *dentry,
643 struct iattr *attr)
644{
645 struct inode *inode = dentry->d_inode;
646 unsigned int ia_valid = attr->ia_valid;
8285fb58 647 bhv_vattr_t vattr = { 0 };
1da177e4
LT
648 int flags = 0;
649 int error;
650
1da177e4
LT
651 if (ia_valid & ATTR_UID) {
652 vattr.va_mask |= XFS_AT_UID;
653 vattr.va_uid = attr->ia_uid;
654 }
655 if (ia_valid & ATTR_GID) {
656 vattr.va_mask |= XFS_AT_GID;
657 vattr.va_gid = attr->ia_gid;
658 }
659 if (ia_valid & ATTR_SIZE) {
660 vattr.va_mask |= XFS_AT_SIZE;
661 vattr.va_size = attr->ia_size;
662 }
663 if (ia_valid & ATTR_ATIME) {
664 vattr.va_mask |= XFS_AT_ATIME;
665 vattr.va_atime = attr->ia_atime;
8c0b5113 666 inode->i_atime = attr->ia_atime;
1da177e4
LT
667 }
668 if (ia_valid & ATTR_MTIME) {
669 vattr.va_mask |= XFS_AT_MTIME;
670 vattr.va_mtime = attr->ia_mtime;
671 }
672 if (ia_valid & ATTR_CTIME) {
673 vattr.va_mask |= XFS_AT_CTIME;
674 vattr.va_ctime = attr->ia_ctime;
675 }
676 if (ia_valid & ATTR_MODE) {
677 vattr.va_mask |= XFS_AT_MODE;
678 vattr.va_mode = attr->ia_mode;
679 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
680 inode->i_mode &= ~S_ISGID;
681 }
682
683 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
684 flags |= ATTR_UTIME;
685#ifdef ATTR_NO_BLOCK
686 if ((ia_valid & ATTR_NO_BLOCK))
687 flags |= ATTR_NONBLOCK;
688#endif
689
739bfb2a 690 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
220b5284 691 if (likely(!error))
21a62542 692 vn_revalidate(vn_from_inode(inode));
220b5284 693 return -error;
1da177e4
LT
694}
695
696STATIC void
416c6d5b 697xfs_vn_truncate(
1da177e4
LT
698 struct inode *inode)
699{
c2536668 700 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
1da177e4
LT
701}
702
703STATIC int
416c6d5b 704xfs_vn_setxattr(
1da177e4
LT
705 struct dentry *dentry,
706 const char *name,
707 const void *data,
708 size_t size,
709 int flags)
710{
67fcaa73 711 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
712 char *attr = (char *)name;
713 attrnames_t *namesp;
714 int xflags = 0;
715 int error;
716
717 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
718 if (!namesp)
719 return -EOPNOTSUPP;
720 attr += namesp->attr_namelen;
721 error = namesp->attr_capable(vp, NULL);
722 if (error)
723 return error;
724
725 /* Convert Linux syscall to XFS internal ATTR flags */
726 if (flags & XATTR_CREATE)
727 xflags |= ATTR_CREATE;
728 if (flags & XATTR_REPLACE)
729 xflags |= ATTR_REPLACE;
730 xflags |= namesp->attr_flag;
731 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
732}
733
734STATIC ssize_t
416c6d5b 735xfs_vn_getxattr(
1da177e4
LT
736 struct dentry *dentry,
737 const char *name,
738 void *data,
739 size_t size)
740{
67fcaa73 741 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
742 char *attr = (char *)name;
743 attrnames_t *namesp;
744 int xflags = 0;
745 ssize_t error;
746
747 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
748 if (!namesp)
749 return -EOPNOTSUPP;
750 attr += namesp->attr_namelen;
751 error = namesp->attr_capable(vp, NULL);
752 if (error)
753 return error;
754
755 /* Convert Linux syscall to XFS internal ATTR flags */
756 if (!size) {
757 xflags |= ATTR_KERNOVAL;
758 data = NULL;
759 }
760 xflags |= namesp->attr_flag;
761 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
762}
763
764STATIC ssize_t
416c6d5b 765xfs_vn_listxattr(
1da177e4
LT
766 struct dentry *dentry,
767 char *data,
768 size_t size)
769{
67fcaa73 770 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
771 int error, xflags = ATTR_KERNAMELS;
772 ssize_t result;
773
774 if (!size)
775 xflags |= ATTR_KERNOVAL;
776 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
777
778 error = attr_generic_list(vp, data, size, xflags, &result);
779 if (error < 0)
780 return error;
781 return result;
782}
783
784STATIC int
416c6d5b 785xfs_vn_removexattr(
1da177e4
LT
786 struct dentry *dentry,
787 const char *name)
788{
67fcaa73 789 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
790 char *attr = (char *)name;
791 attrnames_t *namesp;
792 int xflags = 0;
793 int error;
794
795 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
796 if (!namesp)
797 return -EOPNOTSUPP;
798 attr += namesp->attr_namelen;
799 error = namesp->attr_capable(vp, NULL);
800 if (error)
801 return error;
802 xflags |= namesp->attr_flag;
803 return namesp->attr_remove(vp, attr, xflags);
804}
805
3ed65264
DC
806STATIC long
807xfs_vn_fallocate(
808 struct inode *inode,
809 int mode,
810 loff_t offset,
811 loff_t len)
812{
813 long error;
814 loff_t new_size = 0;
815 xfs_flock64_t bf;
816 xfs_inode_t *ip = XFS_I(inode);
817
818 /* preallocation on directories not yet supported */
819 error = -ENODEV;
820 if (S_ISDIR(inode->i_mode))
821 goto out_error;
822
823 bf.l_whence = 0;
824 bf.l_start = offset;
825 bf.l_len = len;
826
827 xfs_ilock(ip, XFS_IOLOCK_EXCL);
828 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
829 0, NULL, ATTR_NOLOCK);
830 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
831 offset + len > i_size_read(inode))
832 new_size = offset + len;
833
834 /* Change file size if needed */
835 if (new_size) {
836 bhv_vattr_t va;
837
838 va.va_mask = XFS_AT_SIZE;
839 va.va_size = new_size;
840 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
841 }
842
843 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
844out_error:
845 return error;
846}
1da177e4 847
c5ef1c42 848const struct inode_operations xfs_inode_operations = {
416c6d5b
NS
849 .permission = xfs_vn_permission,
850 .truncate = xfs_vn_truncate,
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,
3ed65264 857 .fallocate = xfs_vn_fallocate,
1da177e4
LT
858};
859
c5ef1c42 860const struct inode_operations xfs_dir_inode_operations = {
416c6d5b
NS
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,
1da177e4
LT
877};
878
c5ef1c42 879const struct inode_operations xfs_symlink_inode_operations = {
1da177e4 880 .readlink = generic_readlink,
416c6d5b
NS
881 .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,
1da177e4 890};
This page took 0.398381 seconds and 5 git commands to generate.