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