xfs_iops.c 32 KB

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