xfs_iops.c 19 KB

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