xfs_iops.c 20 KB

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