xfs_iops.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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_dir.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_alloc.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_quota.h"
  31. #include "xfs_mount.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir_sf.h"
  36. #include "xfs_dir2_sf.h"
  37. #include "xfs_attr_sf.h"
  38. #include "xfs_dinode.h"
  39. #include "xfs_inode.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_btree.h"
  42. #include "xfs_ialloc.h"
  43. #include "xfs_rtalloc.h"
  44. #include "xfs_error.h"
  45. #include "xfs_itable.h"
  46. #include "xfs_rw.h"
  47. #include "xfs_acl.h"
  48. #include "xfs_cap.h"
  49. #include "xfs_mac.h"
  50. #include "xfs_attr.h"
  51. #include "xfs_buf_item.h"
  52. #include "xfs_utils.h"
  53. #include <linux/xattr.h>
  54. #include <linux/namei.h>
  55. #define IS_NOATIME(inode) ((inode->i_sb->s_flags & MS_NOATIME) || \
  56. (S_ISDIR(inode->i_mode) && inode->i_sb->s_flags & MS_NODIRATIME))
  57. /*
  58. * Change the requested timestamp in the given inode.
  59. * We don't lock across timestamp updates, and we don't log them but
  60. * we do record the fact that there is dirty information in core.
  61. *
  62. * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
  63. * with XFS_ICHGTIME_ACC to be sure that access time
  64. * update will take. Calling first with XFS_ICHGTIME_ACC
  65. * and then XFS_ICHGTIME_MOD may fail to modify the access
  66. * timestamp if the filesystem is mounted noacctm.
  67. */
  68. void
  69. xfs_ichgtime(
  70. xfs_inode_t *ip,
  71. int flags)
  72. {
  73. struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
  74. timespec_t tv;
  75. /*
  76. * We're not supposed to change timestamps in readonly-mounted
  77. * filesystems. Throw it away if anyone asks us.
  78. */
  79. if (unlikely(IS_RDONLY(inode)))
  80. return;
  81. /*
  82. * Don't update access timestamps on reads if mounted "noatime".
  83. * Throw it away if anyone asks us.
  84. */
  85. if (unlikely(
  86. (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
  87. (flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
  88. XFS_ICHGTIME_ACC))
  89. return;
  90. nanotime(&tv);
  91. if (flags & XFS_ICHGTIME_MOD) {
  92. inode->i_mtime = tv;
  93. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  94. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  95. }
  96. if (flags & XFS_ICHGTIME_ACC) {
  97. inode->i_atime = tv;
  98. ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
  99. ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
  100. }
  101. if (flags & XFS_ICHGTIME_CHG) {
  102. inode->i_ctime = tv;
  103. ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
  104. ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
  105. }
  106. /*
  107. * We update the i_update_core field _after_ changing
  108. * the timestamps in order to coordinate properly with
  109. * xfs_iflush() so that we don't lose timestamp updates.
  110. * This keeps us from having to hold the inode lock
  111. * while doing this. We use the SYNCHRONIZE macro to
  112. * ensure that the compiler does not reorder the update
  113. * of i_update_core above the timestamp updates above.
  114. */
  115. SYNCHRONIZE();
  116. ip->i_update_core = 1;
  117. if (!(inode->i_state & I_LOCK))
  118. mark_inode_dirty_sync(inode);
  119. }
  120. /*
  121. * Variant on the above which avoids querying the system clock
  122. * in situations where we know the Linux inode timestamps have
  123. * just been updated (and so we can update our inode cheaply).
  124. * We also skip the readonly and noatime checks here, they are
  125. * also catered for already.
  126. */
  127. void
  128. xfs_ichgtime_fast(
  129. xfs_inode_t *ip,
  130. struct inode *inode,
  131. int flags)
  132. {
  133. timespec_t *tvp;
  134. /*
  135. * We're not supposed to change timestamps in readonly-mounted
  136. * filesystems. Throw it away if anyone asks us.
  137. */
  138. if (unlikely(IS_RDONLY(inode)))
  139. return;
  140. /*
  141. * Don't update access timestamps on reads if mounted "noatime".
  142. * Throw it away if anyone asks us.
  143. */
  144. if (unlikely(
  145. (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
  146. ((flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
  147. XFS_ICHGTIME_ACC)))
  148. return;
  149. if (flags & XFS_ICHGTIME_MOD) {
  150. tvp = &inode->i_mtime;
  151. ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
  152. ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
  153. }
  154. if (flags & XFS_ICHGTIME_ACC) {
  155. tvp = &inode->i_atime;
  156. ip->i_d.di_atime.t_sec = (__int32_t)tvp->tv_sec;
  157. ip->i_d.di_atime.t_nsec = (__int32_t)tvp->tv_nsec;
  158. }
  159. if (flags & XFS_ICHGTIME_CHG) {
  160. tvp = &inode->i_ctime;
  161. ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
  162. ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
  163. }
  164. /*
  165. * We update the i_update_core field _after_ changing
  166. * the timestamps in order to coordinate properly with
  167. * xfs_iflush() so that we don't lose timestamp updates.
  168. * This keeps us from having to hold the inode lock
  169. * while doing this. We use the SYNCHRONIZE macro to
  170. * ensure that the compiler does not reorder the update
  171. * of i_update_core above the timestamp updates above.
  172. */
  173. SYNCHRONIZE();
  174. ip->i_update_core = 1;
  175. if (!(inode->i_state & I_LOCK))
  176. mark_inode_dirty_sync(inode);
  177. }
  178. /*
  179. * Pull the link count and size up from the xfs inode to the linux inode
  180. */
  181. STATIC void
  182. validate_fields(
  183. struct inode *ip)
  184. {
  185. vnode_t *vp = LINVFS_GET_VP(ip);
  186. vattr_t va;
  187. int error;
  188. va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
  189. VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
  190. if (likely(!error)) {
  191. ip->i_nlink = va.va_nlink;
  192. ip->i_blocks = va.va_nblocks;
  193. /* we're under i_mutex so i_size can't change under us */
  194. if (i_size_read(ip) != va.va_size)
  195. i_size_write(ip, va.va_size);
  196. }
  197. }
  198. /*
  199. * Determine whether a process has a valid fs_struct (kernel daemons
  200. * like knfsd don't have an fs_struct).
  201. *
  202. * XXX(hch): nfsd is broken, better fix it instead.
  203. */
  204. STATIC inline int
  205. has_fs_struct(struct task_struct *task)
  206. {
  207. return (task->fs != init_task.fs);
  208. }
  209. STATIC int
  210. linvfs_mknod(
  211. struct inode *dir,
  212. struct dentry *dentry,
  213. int mode,
  214. dev_t rdev)
  215. {
  216. struct inode *ip;
  217. vattr_t va;
  218. vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
  219. xfs_acl_t *default_acl = NULL;
  220. attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
  221. int error;
  222. /*
  223. * Irix uses Missed'em'V split, but doesn't want to see
  224. * the upper 5 bits of (14bit) major.
  225. */
  226. if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
  227. return -EINVAL;
  228. if (test_default_acl && test_default_acl(dvp)) {
  229. if (!_ACL_ALLOC(default_acl))
  230. return -ENOMEM;
  231. if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
  232. _ACL_FREE(default_acl);
  233. default_acl = NULL;
  234. }
  235. }
  236. if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
  237. mode &= ~current->fs->umask;
  238. memset(&va, 0, sizeof(va));
  239. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  240. va.va_mode = mode;
  241. switch (mode & S_IFMT) {
  242. case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  243. va.va_rdev = sysv_encode_dev(rdev);
  244. va.va_mask |= XFS_AT_RDEV;
  245. /*FALLTHROUGH*/
  246. case S_IFREG:
  247. VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
  248. break;
  249. case S_IFDIR:
  250. VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
  251. break;
  252. default:
  253. error = EINVAL;
  254. break;
  255. }
  256. if (default_acl) {
  257. if (!error) {
  258. error = _ACL_INHERIT(vp, &va, default_acl);
  259. if (!error) {
  260. VMODIFY(vp);
  261. } else {
  262. struct dentry teardown = {};
  263. int err2;
  264. /* Oh, the horror.
  265. * If we can't add the ACL we must back out.
  266. * ENOSPC can hit here, among other things.
  267. */
  268. teardown.d_inode = ip = LINVFS_GET_IP(vp);
  269. teardown.d_name = dentry->d_name;
  270. vn_mark_bad(vp);
  271. if (S_ISDIR(mode))
  272. VOP_RMDIR(dvp, &teardown, NULL, err2);
  273. else
  274. VOP_REMOVE(dvp, &teardown, NULL, err2);
  275. VN_RELE(vp);
  276. }
  277. }
  278. _ACL_FREE(default_acl);
  279. }
  280. if (!error) {
  281. ASSERT(vp);
  282. ip = LINVFS_GET_IP(vp);
  283. if (S_ISCHR(mode) || S_ISBLK(mode))
  284. ip->i_rdev = rdev;
  285. else if (S_ISDIR(mode))
  286. validate_fields(ip);
  287. d_instantiate(dentry, ip);
  288. validate_fields(dir);
  289. }
  290. return -error;
  291. }
  292. STATIC int
  293. linvfs_create(
  294. struct inode *dir,
  295. struct dentry *dentry,
  296. int mode,
  297. struct nameidata *nd)
  298. {
  299. return linvfs_mknod(dir, dentry, mode, 0);
  300. }
  301. STATIC int
  302. linvfs_mkdir(
  303. struct inode *dir,
  304. struct dentry *dentry,
  305. int mode)
  306. {
  307. return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
  308. }
  309. STATIC struct dentry *
  310. linvfs_lookup(
  311. struct inode *dir,
  312. struct dentry *dentry,
  313. struct nameidata *nd)
  314. {
  315. struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
  316. int error;
  317. if (dentry->d_name.len >= MAXNAMELEN)
  318. return ERR_PTR(-ENAMETOOLONG);
  319. VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
  320. if (error) {
  321. if (unlikely(error != ENOENT))
  322. return ERR_PTR(-error);
  323. d_add(dentry, NULL);
  324. return NULL;
  325. }
  326. return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
  327. }
  328. STATIC int
  329. linvfs_link(
  330. struct dentry *old_dentry,
  331. struct inode *dir,
  332. struct dentry *dentry)
  333. {
  334. struct inode *ip; /* inode of guy being linked to */
  335. vnode_t *tdvp; /* target directory for new name/link */
  336. vnode_t *vp; /* vp of name being linked */
  337. int error;
  338. ip = old_dentry->d_inode; /* inode being linked to */
  339. if (S_ISDIR(ip->i_mode))
  340. return -EPERM;
  341. tdvp = LINVFS_GET_VP(dir);
  342. vp = LINVFS_GET_VP(ip);
  343. VOP_LINK(tdvp, vp, dentry, NULL, error);
  344. if (!error) {
  345. VMODIFY(tdvp);
  346. VN_HOLD(vp);
  347. validate_fields(ip);
  348. d_instantiate(dentry, ip);
  349. }
  350. return -error;
  351. }
  352. STATIC int
  353. linvfs_unlink(
  354. struct inode *dir,
  355. struct dentry *dentry)
  356. {
  357. struct inode *inode;
  358. vnode_t *dvp; /* directory containing name to remove */
  359. int error;
  360. inode = dentry->d_inode;
  361. dvp = LINVFS_GET_VP(dir);
  362. VOP_REMOVE(dvp, dentry, NULL, error);
  363. if (!error) {
  364. validate_fields(dir); /* For size only */
  365. validate_fields(inode);
  366. }
  367. return -error;
  368. }
  369. STATIC int
  370. linvfs_symlink(
  371. struct inode *dir,
  372. struct dentry *dentry,
  373. const char *symname)
  374. {
  375. struct inode *ip;
  376. vattr_t va;
  377. vnode_t *dvp; /* directory containing name of symlink */
  378. vnode_t *cvp; /* used to lookup symlink to put in dentry */
  379. int error;
  380. dvp = LINVFS_GET_VP(dir);
  381. cvp = NULL;
  382. memset(&va, 0, sizeof(va));
  383. va.va_mode = S_IFLNK |
  384. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  385. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  386. error = 0;
  387. VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
  388. if (!error && cvp) {
  389. ip = LINVFS_GET_IP(cvp);
  390. d_instantiate(dentry, ip);
  391. validate_fields(dir);
  392. validate_fields(ip); /* size needs update */
  393. }
  394. return -error;
  395. }
  396. STATIC int
  397. linvfs_rmdir(
  398. struct inode *dir,
  399. struct dentry *dentry)
  400. {
  401. struct inode *inode = dentry->d_inode;
  402. vnode_t *dvp = LINVFS_GET_VP(dir);
  403. int error;
  404. VOP_RMDIR(dvp, dentry, NULL, error);
  405. if (!error) {
  406. validate_fields(inode);
  407. validate_fields(dir);
  408. }
  409. return -error;
  410. }
  411. STATIC int
  412. linvfs_rename(
  413. struct inode *odir,
  414. struct dentry *odentry,
  415. struct inode *ndir,
  416. struct dentry *ndentry)
  417. {
  418. struct inode *new_inode = ndentry->d_inode;
  419. vnode_t *fvp; /* from directory */
  420. vnode_t *tvp; /* target directory */
  421. int error;
  422. fvp = LINVFS_GET_VP(odir);
  423. tvp = LINVFS_GET_VP(ndir);
  424. VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
  425. if (error)
  426. return -error;
  427. if (new_inode)
  428. validate_fields(new_inode);
  429. validate_fields(odir);
  430. if (ndir != odir)
  431. validate_fields(ndir);
  432. return 0;
  433. }
  434. /*
  435. * careful here - this function can get called recursively, so
  436. * we need to be very careful about how much stack we use.
  437. * uio is kmalloced for this reason...
  438. */
  439. STATIC void *
  440. linvfs_follow_link(
  441. struct dentry *dentry,
  442. struct nameidata *nd)
  443. {
  444. vnode_t *vp;
  445. uio_t *uio;
  446. iovec_t iov;
  447. int error;
  448. char *link;
  449. ASSERT(dentry);
  450. ASSERT(nd);
  451. link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
  452. if (!link) {
  453. nd_set_link(nd, ERR_PTR(-ENOMEM));
  454. return NULL;
  455. }
  456. uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
  457. if (!uio) {
  458. kfree(link);
  459. nd_set_link(nd, ERR_PTR(-ENOMEM));
  460. return NULL;
  461. }
  462. vp = LINVFS_GET_VP(dentry->d_inode);
  463. iov.iov_base = link;
  464. iov.iov_len = MAXNAMELEN;
  465. uio->uio_iov = &iov;
  466. uio->uio_offset = 0;
  467. uio->uio_segflg = UIO_SYSSPACE;
  468. uio->uio_resid = MAXNAMELEN;
  469. uio->uio_iovcnt = 1;
  470. VOP_READLINK(vp, uio, 0, NULL, error);
  471. if (error) {
  472. kfree(link);
  473. link = ERR_PTR(-error);
  474. } else {
  475. link[MAXNAMELEN - uio->uio_resid] = '\0';
  476. }
  477. kfree(uio);
  478. nd_set_link(nd, link);
  479. return NULL;
  480. }
  481. STATIC void
  482. linvfs_put_link(
  483. struct dentry *dentry,
  484. struct nameidata *nd,
  485. void *p)
  486. {
  487. char *s = nd_get_link(nd);
  488. if (!IS_ERR(s))
  489. kfree(s);
  490. }
  491. #ifdef CONFIG_XFS_POSIX_ACL
  492. STATIC int
  493. linvfs_permission(
  494. struct inode *inode,
  495. int mode,
  496. struct nameidata *nd)
  497. {
  498. vnode_t *vp = LINVFS_GET_VP(inode);
  499. int error;
  500. mode <<= 6; /* convert from linux to vnode access bits */
  501. VOP_ACCESS(vp, mode, NULL, error);
  502. return -error;
  503. }
  504. #else
  505. #define linvfs_permission NULL
  506. #endif
  507. STATIC int
  508. linvfs_getattr(
  509. struct vfsmount *mnt,
  510. struct dentry *dentry,
  511. struct kstat *stat)
  512. {
  513. struct inode *inode = dentry->d_inode;
  514. vnode_t *vp = LINVFS_GET_VP(inode);
  515. int error = 0;
  516. if (unlikely(vp->v_flag & VMODIFIED))
  517. error = vn_revalidate(vp);
  518. if (!error)
  519. generic_fillattr(inode, stat);
  520. return 0;
  521. }
  522. STATIC int
  523. linvfs_setattr(
  524. struct dentry *dentry,
  525. struct iattr *attr)
  526. {
  527. struct inode *inode = dentry->d_inode;
  528. unsigned int ia_valid = attr->ia_valid;
  529. vnode_t *vp = LINVFS_GET_VP(inode);
  530. vattr_t vattr;
  531. int flags = 0;
  532. int error;
  533. memset(&vattr, 0, sizeof(vattr_t));
  534. if (ia_valid & ATTR_UID) {
  535. vattr.va_mask |= XFS_AT_UID;
  536. vattr.va_uid = attr->ia_uid;
  537. }
  538. if (ia_valid & ATTR_GID) {
  539. vattr.va_mask |= XFS_AT_GID;
  540. vattr.va_gid = attr->ia_gid;
  541. }
  542. if (ia_valid & ATTR_SIZE) {
  543. vattr.va_mask |= XFS_AT_SIZE;
  544. vattr.va_size = attr->ia_size;
  545. }
  546. if (ia_valid & ATTR_ATIME) {
  547. vattr.va_mask |= XFS_AT_ATIME;
  548. vattr.va_atime = attr->ia_atime;
  549. }
  550. if (ia_valid & ATTR_MTIME) {
  551. vattr.va_mask |= XFS_AT_MTIME;
  552. vattr.va_mtime = attr->ia_mtime;
  553. }
  554. if (ia_valid & ATTR_CTIME) {
  555. vattr.va_mask |= XFS_AT_CTIME;
  556. vattr.va_ctime = attr->ia_ctime;
  557. }
  558. if (ia_valid & ATTR_MODE) {
  559. vattr.va_mask |= XFS_AT_MODE;
  560. vattr.va_mode = attr->ia_mode;
  561. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  562. inode->i_mode &= ~S_ISGID;
  563. }
  564. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  565. flags |= ATTR_UTIME;
  566. #ifdef ATTR_NO_BLOCK
  567. if ((ia_valid & ATTR_NO_BLOCK))
  568. flags |= ATTR_NONBLOCK;
  569. #endif
  570. VOP_SETATTR(vp, &vattr, flags, NULL, error);
  571. if (error)
  572. return -error;
  573. vn_revalidate(vp);
  574. return error;
  575. }
  576. STATIC void
  577. linvfs_truncate(
  578. struct inode *inode)
  579. {
  580. block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
  581. }
  582. STATIC int
  583. linvfs_setxattr(
  584. struct dentry *dentry,
  585. const char *name,
  586. const void *data,
  587. size_t size,
  588. int flags)
  589. {
  590. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  591. char *attr = (char *)name;
  592. attrnames_t *namesp;
  593. int xflags = 0;
  594. int error;
  595. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  596. if (!namesp)
  597. return -EOPNOTSUPP;
  598. attr += namesp->attr_namelen;
  599. error = namesp->attr_capable(vp, NULL);
  600. if (error)
  601. return error;
  602. /* Convert Linux syscall to XFS internal ATTR flags */
  603. if (flags & XATTR_CREATE)
  604. xflags |= ATTR_CREATE;
  605. if (flags & XATTR_REPLACE)
  606. xflags |= ATTR_REPLACE;
  607. xflags |= namesp->attr_flag;
  608. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  609. }
  610. STATIC ssize_t
  611. linvfs_getxattr(
  612. struct dentry *dentry,
  613. const char *name,
  614. void *data,
  615. size_t size)
  616. {
  617. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  618. char *attr = (char *)name;
  619. attrnames_t *namesp;
  620. int xflags = 0;
  621. ssize_t error;
  622. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  623. if (!namesp)
  624. return -EOPNOTSUPP;
  625. attr += namesp->attr_namelen;
  626. error = namesp->attr_capable(vp, NULL);
  627. if (error)
  628. return error;
  629. /* Convert Linux syscall to XFS internal ATTR flags */
  630. if (!size) {
  631. xflags |= ATTR_KERNOVAL;
  632. data = NULL;
  633. }
  634. xflags |= namesp->attr_flag;
  635. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  636. }
  637. STATIC ssize_t
  638. linvfs_listxattr(
  639. struct dentry *dentry,
  640. char *data,
  641. size_t size)
  642. {
  643. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  644. int error, xflags = ATTR_KERNAMELS;
  645. ssize_t result;
  646. if (!size)
  647. xflags |= ATTR_KERNOVAL;
  648. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  649. error = attr_generic_list(vp, data, size, xflags, &result);
  650. if (error < 0)
  651. return error;
  652. return result;
  653. }
  654. STATIC int
  655. linvfs_removexattr(
  656. struct dentry *dentry,
  657. const char *name)
  658. {
  659. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  660. char *attr = (char *)name;
  661. attrnames_t *namesp;
  662. int xflags = 0;
  663. int error;
  664. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  665. if (!namesp)
  666. return -EOPNOTSUPP;
  667. attr += namesp->attr_namelen;
  668. error = namesp->attr_capable(vp, NULL);
  669. if (error)
  670. return error;
  671. xflags |= namesp->attr_flag;
  672. return namesp->attr_remove(vp, attr, xflags);
  673. }
  674. struct inode_operations linvfs_file_inode_operations = {
  675. .permission = linvfs_permission,
  676. .truncate = linvfs_truncate,
  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. };
  684. struct inode_operations linvfs_dir_inode_operations = {
  685. .create = linvfs_create,
  686. .lookup = linvfs_lookup,
  687. .link = linvfs_link,
  688. .unlink = linvfs_unlink,
  689. .symlink = linvfs_symlink,
  690. .mkdir = linvfs_mkdir,
  691. .rmdir = linvfs_rmdir,
  692. .mknod = linvfs_mknod,
  693. .rename = linvfs_rename,
  694. .permission = linvfs_permission,
  695. .getattr = linvfs_getattr,
  696. .setattr = linvfs_setattr,
  697. .setxattr = linvfs_setxattr,
  698. .getxattr = linvfs_getxattr,
  699. .listxattr = linvfs_listxattr,
  700. .removexattr = linvfs_removexattr,
  701. };
  702. struct inode_operations linvfs_symlink_inode_operations = {
  703. .readlink = generic_readlink,
  704. .follow_link = linvfs_follow_link,
  705. .put_link = linvfs_put_link,
  706. .permission = linvfs_permission,
  707. .getattr = linvfs_getattr,
  708. .setattr = linvfs_setattr,
  709. .setxattr = linvfs_setxattr,
  710. .getxattr = linvfs_getxattr,
  711. .listxattr = linvfs_listxattr,
  712. .removexattr = linvfs_removexattr,
  713. };