xfs_iops.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_bmap.h"
  30. #include "xfs_bmap_util.h"
  31. #include "xfs_acl.h"
  32. #include "xfs_quota.h"
  33. #include "xfs_error.h"
  34. #include "xfs_attr.h"
  35. #include "xfs_trans.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_icache.h"
  38. #include "xfs_symlink.h"
  39. #include "xfs_da_btree.h"
  40. #include "xfs_dir2_priv.h"
  41. #include "xfs_dinode.h"
  42. #include "xfs_trans_space.h"
  43. #include <linux/capability.h>
  44. #include <linux/xattr.h>
  45. #include <linux/namei.h>
  46. #include <linux/posix_acl.h>
  47. #include <linux/security.h>
  48. #include <linux/fiemap.h>
  49. #include <linux/slab.h>
  50. /*
  51. * Directories have different lock order w.r.t. mmap_sem compared to regular
  52. * files. This is due to readdir potentially triggering page faults on a user
  53. * buffer inside filldir(), and this happens with the ilock on the directory
  54. * held. For regular files, the lock order is the other way around - the
  55. * mmap_sem is taken during the page fault, and then we lock the ilock to do
  56. * block mapping. Hence we need a different class for the directory ilock so
  57. * that lockdep can tell them apart.
  58. */
  59. static struct lock_class_key xfs_nondir_ilock_class;
  60. static struct lock_class_key xfs_dir_ilock_class;
  61. static int
  62. xfs_initxattrs(
  63. struct inode *inode,
  64. const struct xattr *xattr_array,
  65. void *fs_info)
  66. {
  67. const struct xattr *xattr;
  68. struct xfs_inode *ip = XFS_I(inode);
  69. int error = 0;
  70. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  71. error = xfs_attr_set(ip, xattr->name, xattr->value,
  72. xattr->value_len, ATTR_SECURE);
  73. if (error < 0)
  74. break;
  75. }
  76. return error;
  77. }
  78. /*
  79. * Hook in SELinux. This is not quite correct yet, what we really need
  80. * here (as we do for default ACLs) is a mechanism by which creation of
  81. * these attrs can be journalled at inode creation time (along with the
  82. * inode, of course, such that log replay can't cause these to be lost).
  83. */
  84. STATIC int
  85. xfs_init_security(
  86. struct inode *inode,
  87. struct inode *dir,
  88. const struct qstr *qstr)
  89. {
  90. return security_inode_init_security(inode, dir, qstr,
  91. &xfs_initxattrs, NULL);
  92. }
  93. static void
  94. xfs_dentry_to_name(
  95. struct xfs_name *namep,
  96. struct dentry *dentry,
  97. int mode)
  98. {
  99. namep->name = dentry->d_name.name;
  100. namep->len = dentry->d_name.len;
  101. namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
  102. }
  103. STATIC void
  104. xfs_cleanup_inode(
  105. struct inode *dir,
  106. struct inode *inode,
  107. struct dentry *dentry)
  108. {
  109. struct xfs_name teardown;
  110. /* Oh, the horror.
  111. * If we can't add the ACL or we fail in
  112. * xfs_init_security we must back out.
  113. * ENOSPC can hit here, among other things.
  114. */
  115. xfs_dentry_to_name(&teardown, dentry, 0);
  116. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  117. iput(inode);
  118. }
  119. STATIC int
  120. xfs_vn_mknod(
  121. struct inode *dir,
  122. struct dentry *dentry,
  123. umode_t mode,
  124. dev_t rdev)
  125. {
  126. struct inode *inode;
  127. struct xfs_inode *ip = NULL;
  128. struct posix_acl *default_acl, *acl;
  129. struct xfs_name name;
  130. int error;
  131. /*
  132. * Irix uses Missed'em'V split, but doesn't want to see
  133. * the upper 5 bits of (14bit) major.
  134. */
  135. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  136. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  137. return -EINVAL;
  138. rdev = sysv_encode_dev(rdev);
  139. } else {
  140. rdev = 0;
  141. }
  142. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  143. if (error)
  144. return error;
  145. xfs_dentry_to_name(&name, dentry, mode);
  146. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  147. if (unlikely(error))
  148. goto out_free_acl;
  149. inode = VFS_I(ip);
  150. error = xfs_init_security(inode, dir, &dentry->d_name);
  151. if (unlikely(error))
  152. goto out_cleanup_inode;
  153. #ifdef CONFIG_XFS_POSIX_ACL
  154. if (default_acl) {
  155. error = xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  156. if (error)
  157. goto out_cleanup_inode;
  158. }
  159. if (acl) {
  160. error = xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  161. if (error)
  162. goto out_cleanup_inode;
  163. }
  164. #endif
  165. d_instantiate(dentry, inode);
  166. out_free_acl:
  167. if (default_acl)
  168. posix_acl_release(default_acl);
  169. if (acl)
  170. posix_acl_release(acl);
  171. return -error;
  172. out_cleanup_inode:
  173. xfs_cleanup_inode(dir, inode, dentry);
  174. goto out_free_acl;
  175. }
  176. STATIC int
  177. xfs_vn_create(
  178. struct inode *dir,
  179. struct dentry *dentry,
  180. umode_t mode,
  181. bool flags)
  182. {
  183. return xfs_vn_mknod(dir, dentry, mode, 0);
  184. }
  185. STATIC int
  186. xfs_vn_mkdir(
  187. struct inode *dir,
  188. struct dentry *dentry,
  189. umode_t mode)
  190. {
  191. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  192. }
  193. STATIC struct dentry *
  194. xfs_vn_lookup(
  195. struct inode *dir,
  196. struct dentry *dentry,
  197. unsigned int flags)
  198. {
  199. struct xfs_inode *cip;
  200. struct xfs_name name;
  201. int error;
  202. if (dentry->d_name.len >= MAXNAMELEN)
  203. return ERR_PTR(-ENAMETOOLONG);
  204. xfs_dentry_to_name(&name, dentry, 0);
  205. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  206. if (unlikely(error)) {
  207. if (unlikely(error != ENOENT))
  208. return ERR_PTR(-error);
  209. d_add(dentry, NULL);
  210. return NULL;
  211. }
  212. return d_splice_alias(VFS_I(cip), dentry);
  213. }
  214. STATIC struct dentry *
  215. xfs_vn_ci_lookup(
  216. struct inode *dir,
  217. struct dentry *dentry,
  218. unsigned int flags)
  219. {
  220. struct xfs_inode *ip;
  221. struct xfs_name xname;
  222. struct xfs_name ci_name;
  223. struct qstr dname;
  224. int error;
  225. if (dentry->d_name.len >= MAXNAMELEN)
  226. return ERR_PTR(-ENAMETOOLONG);
  227. xfs_dentry_to_name(&xname, dentry, 0);
  228. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  229. if (unlikely(error)) {
  230. if (unlikely(error != ENOENT))
  231. return ERR_PTR(-error);
  232. /*
  233. * call d_add(dentry, NULL) here when d_drop_negative_children
  234. * is called in xfs_vn_mknod (ie. allow negative dentries
  235. * with CI filesystems).
  236. */
  237. return NULL;
  238. }
  239. /* if exact match, just splice and exit */
  240. if (!ci_name.name)
  241. return d_splice_alias(VFS_I(ip), dentry);
  242. /* else case-insensitive match... */
  243. dname.name = ci_name.name;
  244. dname.len = ci_name.len;
  245. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  246. kmem_free(ci_name.name);
  247. return dentry;
  248. }
  249. STATIC int
  250. xfs_vn_link(
  251. struct dentry *old_dentry,
  252. struct inode *dir,
  253. struct dentry *dentry)
  254. {
  255. struct inode *inode = old_dentry->d_inode;
  256. struct xfs_name name;
  257. int error;
  258. xfs_dentry_to_name(&name, dentry, inode->i_mode);
  259. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  260. if (unlikely(error))
  261. return -error;
  262. ihold(inode);
  263. d_instantiate(dentry, inode);
  264. return 0;
  265. }
  266. STATIC int
  267. xfs_vn_unlink(
  268. struct inode *dir,
  269. struct dentry *dentry)
  270. {
  271. struct xfs_name name;
  272. int error;
  273. xfs_dentry_to_name(&name, dentry, 0);
  274. error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
  275. if (error)
  276. return error;
  277. /*
  278. * With unlink, the VFS makes the dentry "negative": no inode,
  279. * but still hashed. This is incompatible with case-insensitive
  280. * mode, so invalidate (unhash) the dentry in CI-mode.
  281. */
  282. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  283. d_invalidate(dentry);
  284. return 0;
  285. }
  286. STATIC int
  287. xfs_vn_symlink(
  288. struct inode *dir,
  289. struct dentry *dentry,
  290. const char *symname)
  291. {
  292. struct inode *inode;
  293. struct xfs_inode *cip = NULL;
  294. struct xfs_name name;
  295. int error;
  296. umode_t mode;
  297. mode = S_IFLNK |
  298. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  299. xfs_dentry_to_name(&name, dentry, mode);
  300. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
  301. if (unlikely(error))
  302. goto out;
  303. inode = VFS_I(cip);
  304. error = xfs_init_security(inode, dir, &dentry->d_name);
  305. if (unlikely(error))
  306. goto out_cleanup_inode;
  307. d_instantiate(dentry, inode);
  308. return 0;
  309. out_cleanup_inode:
  310. xfs_cleanup_inode(dir, inode, dentry);
  311. out:
  312. return -error;
  313. }
  314. STATIC int
  315. xfs_vn_rename(
  316. struct inode *odir,
  317. struct dentry *odentry,
  318. struct inode *ndir,
  319. struct dentry *ndentry)
  320. {
  321. struct inode *new_inode = ndentry->d_inode;
  322. struct xfs_name oname;
  323. struct xfs_name nname;
  324. xfs_dentry_to_name(&oname, odentry, 0);
  325. xfs_dentry_to_name(&nname, ndentry, odentry->d_inode->i_mode);
  326. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  327. XFS_I(ndir), &nname, new_inode ?
  328. XFS_I(new_inode) : NULL);
  329. }
  330. /*
  331. * careful here - this function can get called recursively, so
  332. * we need to be very careful about how much stack we use.
  333. * uio is kmalloced for this reason...
  334. */
  335. STATIC void *
  336. xfs_vn_follow_link(
  337. struct dentry *dentry,
  338. struct nameidata *nd)
  339. {
  340. char *link;
  341. int error = -ENOMEM;
  342. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  343. if (!link)
  344. goto out_err;
  345. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  346. if (unlikely(error))
  347. goto out_kfree;
  348. nd_set_link(nd, link);
  349. return NULL;
  350. out_kfree:
  351. kfree(link);
  352. out_err:
  353. nd_set_link(nd, ERR_PTR(error));
  354. return NULL;
  355. }
  356. STATIC int
  357. xfs_vn_getattr(
  358. struct vfsmount *mnt,
  359. struct dentry *dentry,
  360. struct kstat *stat)
  361. {
  362. struct inode *inode = dentry->d_inode;
  363. struct xfs_inode *ip = XFS_I(inode);
  364. struct xfs_mount *mp = ip->i_mount;
  365. trace_xfs_getattr(ip);
  366. if (XFS_FORCED_SHUTDOWN(mp))
  367. return -XFS_ERROR(EIO);
  368. stat->size = XFS_ISIZE(ip);
  369. stat->dev = inode->i_sb->s_dev;
  370. stat->mode = ip->i_d.di_mode;
  371. stat->nlink = ip->i_d.di_nlink;
  372. stat->uid = inode->i_uid;
  373. stat->gid = inode->i_gid;
  374. stat->ino = ip->i_ino;
  375. stat->atime = inode->i_atime;
  376. stat->mtime = inode->i_mtime;
  377. stat->ctime = inode->i_ctime;
  378. stat->blocks =
  379. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  380. switch (inode->i_mode & S_IFMT) {
  381. case S_IFBLK:
  382. case S_IFCHR:
  383. stat->blksize = BLKDEV_IOSIZE;
  384. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  385. sysv_minor(ip->i_df.if_u2.if_rdev));
  386. break;
  387. default:
  388. if (XFS_IS_REALTIME_INODE(ip)) {
  389. /*
  390. * If the file blocks are being allocated from a
  391. * realtime volume, then return the inode's realtime
  392. * extent size or the realtime volume's extent size.
  393. */
  394. stat->blksize =
  395. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  396. } else
  397. stat->blksize = xfs_preferred_iosize(mp);
  398. stat->rdev = 0;
  399. break;
  400. }
  401. return 0;
  402. }
  403. static void
  404. xfs_setattr_mode(
  405. struct xfs_inode *ip,
  406. struct iattr *iattr)
  407. {
  408. struct inode *inode = VFS_I(ip);
  409. umode_t mode = iattr->ia_mode;
  410. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  411. ip->i_d.di_mode &= S_IFMT;
  412. ip->i_d.di_mode |= mode & ~S_IFMT;
  413. inode->i_mode &= S_IFMT;
  414. inode->i_mode |= mode & ~S_IFMT;
  415. }
  416. static void
  417. xfs_setattr_time(
  418. struct xfs_inode *ip,
  419. struct iattr *iattr)
  420. {
  421. struct inode *inode = VFS_I(ip);
  422. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  423. if (iattr->ia_valid & ATTR_ATIME) {
  424. inode->i_atime = iattr->ia_atime;
  425. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  426. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  427. }
  428. if (iattr->ia_valid & ATTR_CTIME) {
  429. inode->i_ctime = iattr->ia_ctime;
  430. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  431. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  432. }
  433. if (iattr->ia_valid & ATTR_MTIME) {
  434. inode->i_mtime = iattr->ia_mtime;
  435. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  436. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  437. }
  438. }
  439. int
  440. xfs_setattr_nonsize(
  441. struct xfs_inode *ip,
  442. struct iattr *iattr,
  443. int flags)
  444. {
  445. xfs_mount_t *mp = ip->i_mount;
  446. struct inode *inode = VFS_I(ip);
  447. int mask = iattr->ia_valid;
  448. xfs_trans_t *tp;
  449. int error;
  450. kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
  451. kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
  452. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  453. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  454. trace_xfs_setattr(ip);
  455. /* If acls are being inherited, we already have this checked */
  456. if (!(flags & XFS_ATTR_NOACL)) {
  457. if (mp->m_flags & XFS_MOUNT_RDONLY)
  458. return XFS_ERROR(EROFS);
  459. if (XFS_FORCED_SHUTDOWN(mp))
  460. return XFS_ERROR(EIO);
  461. error = -inode_change_ok(inode, iattr);
  462. if (error)
  463. return XFS_ERROR(error);
  464. }
  465. ASSERT((mask & ATTR_SIZE) == 0);
  466. /*
  467. * If disk quotas is on, we make sure that the dquots do exist on disk,
  468. * before we start any other transactions. Trying to do this later
  469. * is messy. We don't care to take a readlock to look at the ids
  470. * in inode here, because we can't hold it across the trans_reserve.
  471. * If the IDs do change before we take the ilock, we're covered
  472. * because the i_*dquot fields will get updated anyway.
  473. */
  474. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  475. uint qflags = 0;
  476. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  477. uid = iattr->ia_uid;
  478. qflags |= XFS_QMOPT_UQUOTA;
  479. } else {
  480. uid = inode->i_uid;
  481. }
  482. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  483. gid = iattr->ia_gid;
  484. qflags |= XFS_QMOPT_GQUOTA;
  485. } else {
  486. gid = inode->i_gid;
  487. }
  488. /*
  489. * We take a reference when we initialize udqp and gdqp,
  490. * so it is important that we never blindly double trip on
  491. * the same variable. See xfs_create() for an example.
  492. */
  493. ASSERT(udqp == NULL);
  494. ASSERT(gdqp == NULL);
  495. error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
  496. xfs_kgid_to_gid(gid),
  497. xfs_get_projid(ip),
  498. qflags, &udqp, &gdqp, NULL);
  499. if (error)
  500. return error;
  501. }
  502. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  503. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  504. if (error)
  505. goto out_dqrele;
  506. xfs_ilock(ip, XFS_ILOCK_EXCL);
  507. /*
  508. * Change file ownership. Must be the owner or privileged.
  509. */
  510. if (mask & (ATTR_UID|ATTR_GID)) {
  511. /*
  512. * These IDs could have changed since we last looked at them.
  513. * But, we're assured that if the ownership did change
  514. * while we didn't have the inode locked, inode's dquot(s)
  515. * would have changed also.
  516. */
  517. iuid = inode->i_uid;
  518. igid = inode->i_gid;
  519. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  520. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  521. /*
  522. * Do a quota reservation only if uid/gid is actually
  523. * going to change.
  524. */
  525. if (XFS_IS_QUOTA_RUNNING(mp) &&
  526. ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
  527. (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
  528. ASSERT(tp);
  529. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  530. NULL, capable(CAP_FOWNER) ?
  531. XFS_QMOPT_FORCE_RES : 0);
  532. if (error) /* out of quota */
  533. goto out_trans_cancel;
  534. }
  535. }
  536. xfs_trans_ijoin(tp, ip, 0);
  537. /*
  538. * Change file ownership. Must be the owner or privileged.
  539. */
  540. if (mask & (ATTR_UID|ATTR_GID)) {
  541. /*
  542. * CAP_FSETID overrides the following restrictions:
  543. *
  544. * The set-user-ID and set-group-ID bits of a file will be
  545. * cleared upon successful return from chown()
  546. */
  547. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  548. !capable(CAP_FSETID))
  549. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  550. /*
  551. * Change the ownerships and register quota modifications
  552. * in the transaction.
  553. */
  554. if (!uid_eq(iuid, uid)) {
  555. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  556. ASSERT(mask & ATTR_UID);
  557. ASSERT(udqp);
  558. olddquot1 = xfs_qm_vop_chown(tp, ip,
  559. &ip->i_udquot, udqp);
  560. }
  561. ip->i_d.di_uid = xfs_kuid_to_uid(uid);
  562. inode->i_uid = uid;
  563. }
  564. if (!gid_eq(igid, gid)) {
  565. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  566. ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
  567. !XFS_IS_PQUOTA_ON(mp));
  568. ASSERT(mask & ATTR_GID);
  569. ASSERT(gdqp);
  570. olddquot2 = xfs_qm_vop_chown(tp, ip,
  571. &ip->i_gdquot, gdqp);
  572. }
  573. ip->i_d.di_gid = xfs_kgid_to_gid(gid);
  574. inode->i_gid = gid;
  575. }
  576. }
  577. if (mask & ATTR_MODE)
  578. xfs_setattr_mode(ip, iattr);
  579. if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  580. xfs_setattr_time(ip, iattr);
  581. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  582. XFS_STATS_INC(xs_ig_attrchg);
  583. if (mp->m_flags & XFS_MOUNT_WSYNC)
  584. xfs_trans_set_sync(tp);
  585. error = xfs_trans_commit(tp, 0);
  586. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  587. /*
  588. * Release any dquot(s) the inode had kept before chown.
  589. */
  590. xfs_qm_dqrele(olddquot1);
  591. xfs_qm_dqrele(olddquot2);
  592. xfs_qm_dqrele(udqp);
  593. xfs_qm_dqrele(gdqp);
  594. if (error)
  595. return XFS_ERROR(error);
  596. /*
  597. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  598. * update. We could avoid this with linked transactions
  599. * and passing down the transaction pointer all the way
  600. * to attr_set. No previous user of the generic
  601. * Posix ACL code seems to care about this issue either.
  602. */
  603. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  604. error = -posix_acl_chmod(inode, inode->i_mode);
  605. if (error)
  606. return XFS_ERROR(error);
  607. }
  608. return 0;
  609. out_trans_cancel:
  610. xfs_trans_cancel(tp, 0);
  611. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  612. out_dqrele:
  613. xfs_qm_dqrele(udqp);
  614. xfs_qm_dqrele(gdqp);
  615. return error;
  616. }
  617. /*
  618. * Truncate file. Must have write permission and not be a directory.
  619. */
  620. int
  621. xfs_setattr_size(
  622. struct xfs_inode *ip,
  623. struct iattr *iattr)
  624. {
  625. struct xfs_mount *mp = ip->i_mount;
  626. struct inode *inode = VFS_I(ip);
  627. xfs_off_t oldsize, newsize;
  628. struct xfs_trans *tp;
  629. int error;
  630. uint lock_flags = 0;
  631. uint commit_flags = 0;
  632. trace_xfs_setattr(ip);
  633. if (mp->m_flags & XFS_MOUNT_RDONLY)
  634. return XFS_ERROR(EROFS);
  635. if (XFS_FORCED_SHUTDOWN(mp))
  636. return XFS_ERROR(EIO);
  637. error = -inode_change_ok(inode, iattr);
  638. if (error)
  639. return XFS_ERROR(error);
  640. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  641. ASSERT(S_ISREG(ip->i_d.di_mode));
  642. ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  643. ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  644. oldsize = inode->i_size;
  645. newsize = iattr->ia_size;
  646. /*
  647. * Short circuit the truncate case for zero length files.
  648. */
  649. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  650. if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
  651. return 0;
  652. /*
  653. * Use the regular setattr path to update the timestamps.
  654. */
  655. iattr->ia_valid &= ~ATTR_SIZE;
  656. return xfs_setattr_nonsize(ip, iattr, 0);
  657. }
  658. /*
  659. * Make sure that the dquots are attached to the inode.
  660. */
  661. error = xfs_qm_dqattach(ip, 0);
  662. if (error)
  663. return error;
  664. /*
  665. * Now we can make the changes. Before we join the inode to the
  666. * transaction, take care of the part of the truncation that must be
  667. * done without the inode lock. This needs to be done before joining
  668. * the inode to the transaction, because the inode cannot be unlocked
  669. * once it is a part of the transaction.
  670. */
  671. if (newsize > oldsize) {
  672. /*
  673. * Do the first part of growing a file: zero any data in the
  674. * last block that is beyond the old EOF. We need to do this
  675. * before the inode is joined to the transaction to modify
  676. * i_size.
  677. */
  678. error = xfs_zero_eof(ip, newsize, oldsize);
  679. if (error)
  680. return error;
  681. }
  682. /*
  683. * We are going to log the inode size change in this transaction so
  684. * any previous writes that are beyond the on disk EOF and the new
  685. * EOF that have not been written out need to be written here. If we
  686. * do not write the data out, we expose ourselves to the null files
  687. * problem.
  688. *
  689. * Only flush from the on disk size to the smaller of the in memory
  690. * file size or the new size as that's the range we really care about
  691. * here and prevents waiting for other data not within the range we
  692. * care about here.
  693. */
  694. if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
  695. error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  696. ip->i_d.di_size, newsize);
  697. if (error)
  698. return error;
  699. }
  700. /*
  701. * Wait for all direct I/O to complete.
  702. */
  703. inode_dio_wait(inode);
  704. error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
  705. if (error)
  706. return error;
  707. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  708. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  709. if (error)
  710. goto out_trans_cancel;
  711. truncate_setsize(inode, newsize);
  712. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  713. lock_flags |= XFS_ILOCK_EXCL;
  714. xfs_ilock(ip, XFS_ILOCK_EXCL);
  715. xfs_trans_ijoin(tp, ip, 0);
  716. /*
  717. * Only change the c/mtime if we are changing the size or we are
  718. * explicitly asked to change it. This handles the semantic difference
  719. * between truncate() and ftruncate() as implemented in the VFS.
  720. *
  721. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  722. * special case where we need to update the times despite not having
  723. * these flags set. For all other operations the VFS set these flags
  724. * explicitly if it wants a timestamp update.
  725. */
  726. if (newsize != oldsize &&
  727. !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
  728. iattr->ia_ctime = iattr->ia_mtime =
  729. current_fs_time(inode->i_sb);
  730. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  731. }
  732. /*
  733. * The first thing we do is set the size to new_size permanently on
  734. * disk. This way we don't have to worry about anyone ever being able
  735. * to look at the data being freed even in the face of a crash.
  736. * What we're getting around here is the case where we free a block, it
  737. * is allocated to another file, it is written to, and then we crash.
  738. * If the new data gets written to the file but the log buffers
  739. * containing the free and reallocation don't, then we'd end up with
  740. * garbage in the blocks being freed. As long as we make the new size
  741. * permanent before actually freeing any blocks it doesn't matter if
  742. * they get written to.
  743. */
  744. ip->i_d.di_size = newsize;
  745. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  746. if (newsize <= oldsize) {
  747. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  748. if (error)
  749. goto out_trans_abort;
  750. /*
  751. * Truncated "down", so we're removing references to old data
  752. * here - if we delay flushing for a long time, we expose
  753. * ourselves unduly to the notorious NULL files problem. So,
  754. * we mark this inode and flush it when the file is closed,
  755. * and do not wait the usual (long) time for writeout.
  756. */
  757. xfs_iflags_set(ip, XFS_ITRUNCATED);
  758. /* A truncate down always removes post-EOF blocks. */
  759. xfs_inode_clear_eofblocks_tag(ip);
  760. }
  761. if (iattr->ia_valid & ATTR_MODE)
  762. xfs_setattr_mode(ip, iattr);
  763. if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  764. xfs_setattr_time(ip, iattr);
  765. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  766. XFS_STATS_INC(xs_ig_attrchg);
  767. if (mp->m_flags & XFS_MOUNT_WSYNC)
  768. xfs_trans_set_sync(tp);
  769. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  770. out_unlock:
  771. if (lock_flags)
  772. xfs_iunlock(ip, lock_flags);
  773. return error;
  774. out_trans_abort:
  775. commit_flags |= XFS_TRANS_ABORT;
  776. out_trans_cancel:
  777. xfs_trans_cancel(tp, commit_flags);
  778. goto out_unlock;
  779. }
  780. STATIC int
  781. xfs_vn_setattr(
  782. struct dentry *dentry,
  783. struct iattr *iattr)
  784. {
  785. struct xfs_inode *ip = XFS_I(dentry->d_inode);
  786. int error;
  787. if (iattr->ia_valid & ATTR_SIZE) {
  788. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  789. error = xfs_setattr_size(ip, iattr);
  790. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  791. } else {
  792. error = xfs_setattr_nonsize(ip, iattr, 0);
  793. }
  794. return -error;
  795. }
  796. STATIC int
  797. xfs_vn_update_time(
  798. struct inode *inode,
  799. struct timespec *now,
  800. int flags)
  801. {
  802. struct xfs_inode *ip = XFS_I(inode);
  803. struct xfs_mount *mp = ip->i_mount;
  804. struct xfs_trans *tp;
  805. int error;
  806. trace_xfs_update_time(ip);
  807. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  808. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
  809. if (error) {
  810. xfs_trans_cancel(tp, 0);
  811. return -error;
  812. }
  813. xfs_ilock(ip, XFS_ILOCK_EXCL);
  814. if (flags & S_CTIME) {
  815. inode->i_ctime = *now;
  816. ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
  817. ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
  818. }
  819. if (flags & S_MTIME) {
  820. inode->i_mtime = *now;
  821. ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
  822. ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
  823. }
  824. if (flags & S_ATIME) {
  825. inode->i_atime = *now;
  826. ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
  827. ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
  828. }
  829. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  830. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  831. return -xfs_trans_commit(tp, 0);
  832. }
  833. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  834. /*
  835. * Call fiemap helper to fill in user data.
  836. * Returns positive errors to xfs_getbmap.
  837. */
  838. STATIC int
  839. xfs_fiemap_format(
  840. void **arg,
  841. struct getbmapx *bmv,
  842. int *full)
  843. {
  844. int error;
  845. struct fiemap_extent_info *fieinfo = *arg;
  846. u32 fiemap_flags = 0;
  847. u64 logical, physical, length;
  848. /* Do nothing for a hole */
  849. if (bmv->bmv_block == -1LL)
  850. return 0;
  851. logical = BBTOB(bmv->bmv_offset);
  852. physical = BBTOB(bmv->bmv_block);
  853. length = BBTOB(bmv->bmv_length);
  854. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  855. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  856. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  857. fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
  858. FIEMAP_EXTENT_UNKNOWN);
  859. physical = 0; /* no block yet */
  860. }
  861. if (bmv->bmv_oflags & BMV_OF_LAST)
  862. fiemap_flags |= FIEMAP_EXTENT_LAST;
  863. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  864. length, fiemap_flags);
  865. if (error > 0) {
  866. error = 0;
  867. *full = 1; /* user array now full */
  868. }
  869. return -error;
  870. }
  871. STATIC int
  872. xfs_vn_fiemap(
  873. struct inode *inode,
  874. struct fiemap_extent_info *fieinfo,
  875. u64 start,
  876. u64 length)
  877. {
  878. xfs_inode_t *ip = XFS_I(inode);
  879. struct getbmapx bm;
  880. int error;
  881. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  882. if (error)
  883. return error;
  884. /* Set up bmap header for xfs internal routine */
  885. bm.bmv_offset = BTOBB(start);
  886. /* Special case for whole file */
  887. if (length == FIEMAP_MAX_OFFSET)
  888. bm.bmv_length = -1LL;
  889. else
  890. bm.bmv_length = BTOBB(length);
  891. /* We add one because in getbmap world count includes the header */
  892. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  893. fieinfo->fi_extents_max + 1;
  894. bm.bmv_count = min_t(__s32, bm.bmv_count,
  895. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  896. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  897. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  898. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  899. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  900. bm.bmv_iflags |= BMV_IF_DELALLOC;
  901. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  902. if (error)
  903. return -error;
  904. return 0;
  905. }
  906. STATIC int
  907. xfs_vn_tmpfile(
  908. struct inode *dir,
  909. struct dentry *dentry,
  910. umode_t mode)
  911. {
  912. int error;
  913. struct xfs_inode *ip;
  914. struct inode *inode;
  915. error = xfs_create_tmpfile(XFS_I(dir), dentry, mode, &ip);
  916. if (unlikely(error))
  917. return -error;
  918. inode = VFS_I(ip);
  919. error = xfs_init_security(inode, dir, &dentry->d_name);
  920. if (unlikely(error)) {
  921. iput(inode);
  922. return -error;
  923. }
  924. d_tmpfile(dentry, inode);
  925. return 0;
  926. }
  927. static const struct inode_operations xfs_inode_operations = {
  928. .get_acl = xfs_get_acl,
  929. .set_acl = xfs_set_acl,
  930. .getattr = xfs_vn_getattr,
  931. .setattr = xfs_vn_setattr,
  932. .setxattr = generic_setxattr,
  933. .getxattr = generic_getxattr,
  934. .removexattr = generic_removexattr,
  935. .listxattr = xfs_vn_listxattr,
  936. .fiemap = xfs_vn_fiemap,
  937. .update_time = xfs_vn_update_time,
  938. };
  939. static const struct inode_operations xfs_dir_inode_operations = {
  940. .create = xfs_vn_create,
  941. .lookup = xfs_vn_lookup,
  942. .link = xfs_vn_link,
  943. .unlink = xfs_vn_unlink,
  944. .symlink = xfs_vn_symlink,
  945. .mkdir = xfs_vn_mkdir,
  946. /*
  947. * Yes, XFS uses the same method for rmdir and unlink.
  948. *
  949. * There are some subtile differences deeper in the code,
  950. * but we use S_ISDIR to check for those.
  951. */
  952. .rmdir = xfs_vn_unlink,
  953. .mknod = xfs_vn_mknod,
  954. .rename = xfs_vn_rename,
  955. .get_acl = xfs_get_acl,
  956. .set_acl = xfs_set_acl,
  957. .getattr = xfs_vn_getattr,
  958. .setattr = xfs_vn_setattr,
  959. .setxattr = generic_setxattr,
  960. .getxattr = generic_getxattr,
  961. .removexattr = generic_removexattr,
  962. .listxattr = xfs_vn_listxattr,
  963. .update_time = xfs_vn_update_time,
  964. .tmpfile = xfs_vn_tmpfile,
  965. };
  966. static const struct inode_operations xfs_dir_ci_inode_operations = {
  967. .create = xfs_vn_create,
  968. .lookup = xfs_vn_ci_lookup,
  969. .link = xfs_vn_link,
  970. .unlink = xfs_vn_unlink,
  971. .symlink = xfs_vn_symlink,
  972. .mkdir = xfs_vn_mkdir,
  973. /*
  974. * Yes, XFS uses the same method for rmdir and unlink.
  975. *
  976. * There are some subtile differences deeper in the code,
  977. * but we use S_ISDIR to check for those.
  978. */
  979. .rmdir = xfs_vn_unlink,
  980. .mknod = xfs_vn_mknod,
  981. .rename = xfs_vn_rename,
  982. .get_acl = xfs_get_acl,
  983. .set_acl = xfs_set_acl,
  984. .getattr = xfs_vn_getattr,
  985. .setattr = xfs_vn_setattr,
  986. .setxattr = generic_setxattr,
  987. .getxattr = generic_getxattr,
  988. .removexattr = generic_removexattr,
  989. .listxattr = xfs_vn_listxattr,
  990. .update_time = xfs_vn_update_time,
  991. .tmpfile = xfs_vn_tmpfile,
  992. };
  993. static const struct inode_operations xfs_symlink_inode_operations = {
  994. .readlink = generic_readlink,
  995. .follow_link = xfs_vn_follow_link,
  996. .put_link = kfree_put_link,
  997. .getattr = xfs_vn_getattr,
  998. .setattr = xfs_vn_setattr,
  999. .setxattr = generic_setxattr,
  1000. .getxattr = generic_getxattr,
  1001. .removexattr = generic_removexattr,
  1002. .listxattr = xfs_vn_listxattr,
  1003. .update_time = xfs_vn_update_time,
  1004. };
  1005. STATIC void
  1006. xfs_diflags_to_iflags(
  1007. struct inode *inode,
  1008. struct xfs_inode *ip)
  1009. {
  1010. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  1011. inode->i_flags |= S_IMMUTABLE;
  1012. else
  1013. inode->i_flags &= ~S_IMMUTABLE;
  1014. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  1015. inode->i_flags |= S_APPEND;
  1016. else
  1017. inode->i_flags &= ~S_APPEND;
  1018. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  1019. inode->i_flags |= S_SYNC;
  1020. else
  1021. inode->i_flags &= ~S_SYNC;
  1022. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  1023. inode->i_flags |= S_NOATIME;
  1024. else
  1025. inode->i_flags &= ~S_NOATIME;
  1026. }
  1027. /*
  1028. * Initialize the Linux inode, set up the operation vectors and
  1029. * unlock the inode.
  1030. *
  1031. * When reading existing inodes from disk this is called directly
  1032. * from xfs_iget, when creating a new inode it is called from
  1033. * xfs_ialloc after setting up the inode.
  1034. *
  1035. * We are always called with an uninitialised linux inode here.
  1036. * We need to initialise the necessary fields and take a reference
  1037. * on it.
  1038. */
  1039. void
  1040. xfs_setup_inode(
  1041. struct xfs_inode *ip)
  1042. {
  1043. struct inode *inode = &ip->i_vnode;
  1044. gfp_t gfp_mask;
  1045. inode->i_ino = ip->i_ino;
  1046. inode->i_state = I_NEW;
  1047. inode_sb_list_add(inode);
  1048. /* make the inode look hashed for the writeback code */
  1049. hlist_add_fake(&inode->i_hash);
  1050. inode->i_mode = ip->i_d.di_mode;
  1051. set_nlink(inode, ip->i_d.di_nlink);
  1052. inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
  1053. inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
  1054. switch (inode->i_mode & S_IFMT) {
  1055. case S_IFBLK:
  1056. case S_IFCHR:
  1057. inode->i_rdev =
  1058. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1059. sysv_minor(ip->i_df.if_u2.if_rdev));
  1060. break;
  1061. default:
  1062. inode->i_rdev = 0;
  1063. break;
  1064. }
  1065. inode->i_generation = ip->i_d.di_gen;
  1066. i_size_write(inode, ip->i_d.di_size);
  1067. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  1068. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  1069. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  1070. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  1071. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  1072. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  1073. xfs_diflags_to_iflags(inode, ip);
  1074. ip->d_ops = ip->i_mount->m_nondir_inode_ops;
  1075. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
  1076. switch (inode->i_mode & S_IFMT) {
  1077. case S_IFREG:
  1078. inode->i_op = &xfs_inode_operations;
  1079. inode->i_fop = &xfs_file_operations;
  1080. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1081. break;
  1082. case S_IFDIR:
  1083. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
  1084. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1085. inode->i_op = &xfs_dir_ci_inode_operations;
  1086. else
  1087. inode->i_op = &xfs_dir_inode_operations;
  1088. inode->i_fop = &xfs_dir_file_operations;
  1089. ip->d_ops = ip->i_mount->m_dir_inode_ops;
  1090. break;
  1091. case S_IFLNK:
  1092. inode->i_op = &xfs_symlink_inode_operations;
  1093. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  1094. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1095. break;
  1096. default:
  1097. inode->i_op = &xfs_inode_operations;
  1098. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1099. break;
  1100. }
  1101. /*
  1102. * Ensure all page cache allocations are done from GFP_NOFS context to
  1103. * prevent direct reclaim recursion back into the filesystem and blowing
  1104. * stacks or deadlocking.
  1105. */
  1106. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  1107. mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
  1108. /*
  1109. * If there is no attribute fork no ACL can exist on this inode,
  1110. * and it can't have any file capabilities attached to it either.
  1111. */
  1112. if (!XFS_IFORK_Q(ip)) {
  1113. inode_has_no_xattr(inode);
  1114. cache_no_acl(inode);
  1115. }
  1116. xfs_iflags_clear(ip, XFS_INEW);
  1117. barrier();
  1118. unlock_new_inode(inode);
  1119. }