xfs_iops.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_acl.h"
  15. #include "xfs_quota.h"
  16. #include "xfs_da_format.h"
  17. #include "xfs_da_btree.h"
  18. #include "xfs_attr.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_icache.h"
  22. #include "xfs_symlink.h"
  23. #include "xfs_dir2.h"
  24. #include "xfs_iomap.h"
  25. #include "xfs_error.h"
  26. #include "xfs_ioctl.h"
  27. #include "xfs_xattr.h"
  28. #include <linux/posix_acl.h>
  29. #include <linux/security.h>
  30. #include <linux/iversion.h>
  31. #include <linux/fiemap.h>
  32. /*
  33. * Directories have different lock order w.r.t. mmap_lock compared to regular
  34. * files. This is due to readdir potentially triggering page faults on a user
  35. * buffer inside filldir(), and this happens with the ilock on the directory
  36. * held. For regular files, the lock order is the other way around - the
  37. * mmap_lock is taken during the page fault, and then we lock the ilock to do
  38. * block mapping. Hence we need a different class for the directory ilock so
  39. * that lockdep can tell them apart.
  40. */
  41. static struct lock_class_key xfs_nondir_ilock_class;
  42. static struct lock_class_key xfs_dir_ilock_class;
  43. static int
  44. xfs_initxattrs(
  45. struct inode *inode,
  46. const struct xattr *xattr_array,
  47. void *fs_info)
  48. {
  49. const struct xattr *xattr;
  50. struct xfs_inode *ip = XFS_I(inode);
  51. int error = 0;
  52. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  53. struct xfs_da_args args = {
  54. .dp = ip,
  55. .attr_filter = XFS_ATTR_SECURE,
  56. .name = xattr->name,
  57. .namelen = strlen(xattr->name),
  58. .value = xattr->value,
  59. .valuelen = xattr->value_len,
  60. };
  61. error = xfs_attr_change(&args);
  62. if (error < 0)
  63. break;
  64. }
  65. return error;
  66. }
  67. /*
  68. * Hook in SELinux. This is not quite correct yet, what we really need
  69. * here (as we do for default ACLs) is a mechanism by which creation of
  70. * these attrs can be journalled at inode creation time (along with the
  71. * inode, of course, such that log replay can't cause these to be lost).
  72. */
  73. int
  74. xfs_inode_init_security(
  75. struct inode *inode,
  76. struct inode *dir,
  77. const struct qstr *qstr)
  78. {
  79. return security_inode_init_security(inode, dir, qstr,
  80. &xfs_initxattrs, NULL);
  81. }
  82. static void
  83. xfs_dentry_to_name(
  84. struct xfs_name *namep,
  85. struct dentry *dentry)
  86. {
  87. namep->name = dentry->d_name.name;
  88. namep->len = dentry->d_name.len;
  89. namep->type = XFS_DIR3_FT_UNKNOWN;
  90. }
  91. static int
  92. xfs_dentry_mode_to_name(
  93. struct xfs_name *namep,
  94. struct dentry *dentry,
  95. int mode)
  96. {
  97. namep->name = dentry->d_name.name;
  98. namep->len = dentry->d_name.len;
  99. namep->type = xfs_mode_to_ftype(mode);
  100. if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
  101. return -EFSCORRUPTED;
  102. return 0;
  103. }
  104. STATIC void
  105. xfs_cleanup_inode(
  106. struct inode *dir,
  107. struct inode *inode,
  108. struct dentry *dentry)
  109. {
  110. struct xfs_name teardown;
  111. /* Oh, the horror.
  112. * If we can't add the ACL or we fail in
  113. * xfs_inode_init_security we must back out.
  114. * ENOSPC can hit here, among other things.
  115. */
  116. xfs_dentry_to_name(&teardown, dentry);
  117. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  118. }
  119. /*
  120. * Check to see if we are likely to need an extended attribute to be added to
  121. * the inode we are about to allocate. This allows the attribute fork to be
  122. * created during the inode allocation, reducing the number of transactions we
  123. * need to do in this fast path.
  124. *
  125. * The security checks are optimistic, but not guaranteed. The two LSMs that
  126. * require xattrs to be added here (selinux and smack) are also the only two
  127. * LSMs that add a sb->s_security structure to the superblock. Hence if security
  128. * is enabled and sb->s_security is set, we have a pretty good idea that we are
  129. * going to be asked to add a security xattr immediately after allocating the
  130. * xfs inode and instantiating the VFS inode.
  131. */
  132. static inline bool
  133. xfs_create_need_xattr(
  134. struct inode *dir,
  135. struct posix_acl *default_acl,
  136. struct posix_acl *acl)
  137. {
  138. if (acl)
  139. return true;
  140. if (default_acl)
  141. return true;
  142. #if IS_ENABLED(CONFIG_SECURITY)
  143. if (dir->i_sb->s_security)
  144. return true;
  145. #endif
  146. return false;
  147. }
  148. STATIC int
  149. xfs_generic_create(
  150. struct user_namespace *mnt_userns,
  151. struct inode *dir,
  152. struct dentry *dentry,
  153. umode_t mode,
  154. dev_t rdev,
  155. struct file *tmpfile) /* unnamed file */
  156. {
  157. struct inode *inode;
  158. struct xfs_inode *ip = NULL;
  159. struct posix_acl *default_acl, *acl;
  160. struct xfs_name name;
  161. int error;
  162. /*
  163. * Irix uses Missed'em'V split, but doesn't want to see
  164. * the upper 5 bits of (14bit) major.
  165. */
  166. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  167. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  168. return -EINVAL;
  169. } else {
  170. rdev = 0;
  171. }
  172. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  173. if (error)
  174. return error;
  175. /* Verify mode is valid also for tmpfile case */
  176. error = xfs_dentry_mode_to_name(&name, dentry, mode);
  177. if (unlikely(error))
  178. goto out_free_acl;
  179. if (!tmpfile) {
  180. error = xfs_create(mnt_userns, XFS_I(dir), &name, mode, rdev,
  181. xfs_create_need_xattr(dir, default_acl, acl),
  182. &ip);
  183. } else {
  184. error = xfs_create_tmpfile(mnt_userns, XFS_I(dir), mode, &ip);
  185. }
  186. if (unlikely(error))
  187. goto out_free_acl;
  188. inode = VFS_I(ip);
  189. error = xfs_inode_init_security(inode, dir, &dentry->d_name);
  190. if (unlikely(error))
  191. goto out_cleanup_inode;
  192. if (default_acl) {
  193. error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  194. if (error)
  195. goto out_cleanup_inode;
  196. }
  197. if (acl) {
  198. error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  199. if (error)
  200. goto out_cleanup_inode;
  201. }
  202. xfs_setup_iops(ip);
  203. if (tmpfile) {
  204. /*
  205. * The VFS requires that any inode fed to d_tmpfile must have
  206. * nlink == 1 so that it can decrement the nlink in d_tmpfile.
  207. * However, we created the temp file with nlink == 0 because
  208. * we're not allowed to put an inode with nlink > 0 on the
  209. * unlinked list. Therefore we have to set nlink to 1 so that
  210. * d_tmpfile can immediately set it back to zero.
  211. */
  212. set_nlink(inode, 1);
  213. d_tmpfile(tmpfile, inode);
  214. } else
  215. d_instantiate(dentry, inode);
  216. xfs_finish_inode_setup(ip);
  217. out_free_acl:
  218. posix_acl_release(default_acl);
  219. posix_acl_release(acl);
  220. return error;
  221. out_cleanup_inode:
  222. xfs_finish_inode_setup(ip);
  223. if (!tmpfile)
  224. xfs_cleanup_inode(dir, inode, dentry);
  225. xfs_irele(ip);
  226. goto out_free_acl;
  227. }
  228. STATIC int
  229. xfs_vn_mknod(
  230. struct user_namespace *mnt_userns,
  231. struct inode *dir,
  232. struct dentry *dentry,
  233. umode_t mode,
  234. dev_t rdev)
  235. {
  236. return xfs_generic_create(mnt_userns, dir, dentry, mode, rdev, NULL);
  237. }
  238. STATIC int
  239. xfs_vn_create(
  240. struct user_namespace *mnt_userns,
  241. struct inode *dir,
  242. struct dentry *dentry,
  243. umode_t mode,
  244. bool flags)
  245. {
  246. return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, NULL);
  247. }
  248. STATIC int
  249. xfs_vn_mkdir(
  250. struct user_namespace *mnt_userns,
  251. struct inode *dir,
  252. struct dentry *dentry,
  253. umode_t mode)
  254. {
  255. return xfs_generic_create(mnt_userns, dir, dentry, mode | S_IFDIR, 0,
  256. NULL);
  257. }
  258. STATIC struct dentry *
  259. xfs_vn_lookup(
  260. struct inode *dir,
  261. struct dentry *dentry,
  262. unsigned int flags)
  263. {
  264. struct inode *inode;
  265. struct xfs_inode *cip;
  266. struct xfs_name name;
  267. int error;
  268. if (dentry->d_name.len >= MAXNAMELEN)
  269. return ERR_PTR(-ENAMETOOLONG);
  270. xfs_dentry_to_name(&name, dentry);
  271. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  272. if (likely(!error))
  273. inode = VFS_I(cip);
  274. else if (likely(error == -ENOENT))
  275. inode = NULL;
  276. else
  277. inode = ERR_PTR(error);
  278. return d_splice_alias(inode, dentry);
  279. }
  280. STATIC struct dentry *
  281. xfs_vn_ci_lookup(
  282. struct inode *dir,
  283. struct dentry *dentry,
  284. unsigned int flags)
  285. {
  286. struct xfs_inode *ip;
  287. struct xfs_name xname;
  288. struct xfs_name ci_name;
  289. struct qstr dname;
  290. int error;
  291. if (dentry->d_name.len >= MAXNAMELEN)
  292. return ERR_PTR(-ENAMETOOLONG);
  293. xfs_dentry_to_name(&xname, dentry);
  294. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  295. if (unlikely(error)) {
  296. if (unlikely(error != -ENOENT))
  297. return ERR_PTR(error);
  298. /*
  299. * call d_add(dentry, NULL) here when d_drop_negative_children
  300. * is called in xfs_vn_mknod (ie. allow negative dentries
  301. * with CI filesystems).
  302. */
  303. return NULL;
  304. }
  305. /* if exact match, just splice and exit */
  306. if (!ci_name.name)
  307. return d_splice_alias(VFS_I(ip), dentry);
  308. /* else case-insensitive match... */
  309. dname.name = ci_name.name;
  310. dname.len = ci_name.len;
  311. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  312. kmem_free(ci_name.name);
  313. return dentry;
  314. }
  315. STATIC int
  316. xfs_vn_link(
  317. struct dentry *old_dentry,
  318. struct inode *dir,
  319. struct dentry *dentry)
  320. {
  321. struct inode *inode = d_inode(old_dentry);
  322. struct xfs_name name;
  323. int error;
  324. error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
  325. if (unlikely(error))
  326. return error;
  327. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  328. if (unlikely(error))
  329. return error;
  330. ihold(inode);
  331. d_instantiate(dentry, inode);
  332. return 0;
  333. }
  334. STATIC int
  335. xfs_vn_unlink(
  336. struct inode *dir,
  337. struct dentry *dentry)
  338. {
  339. struct xfs_name name;
  340. int error;
  341. xfs_dentry_to_name(&name, dentry);
  342. error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
  343. if (error)
  344. return error;
  345. /*
  346. * With unlink, the VFS makes the dentry "negative": no inode,
  347. * but still hashed. This is incompatible with case-insensitive
  348. * mode, so invalidate (unhash) the dentry in CI-mode.
  349. */
  350. if (xfs_has_asciici(XFS_M(dir->i_sb)))
  351. d_invalidate(dentry);
  352. return 0;
  353. }
  354. STATIC int
  355. xfs_vn_symlink(
  356. struct user_namespace *mnt_userns,
  357. struct inode *dir,
  358. struct dentry *dentry,
  359. const char *symname)
  360. {
  361. struct inode *inode;
  362. struct xfs_inode *cip = NULL;
  363. struct xfs_name name;
  364. int error;
  365. umode_t mode;
  366. mode = S_IFLNK |
  367. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  368. error = xfs_dentry_mode_to_name(&name, dentry, mode);
  369. if (unlikely(error))
  370. goto out;
  371. error = xfs_symlink(mnt_userns, XFS_I(dir), &name, symname, mode, &cip);
  372. if (unlikely(error))
  373. goto out;
  374. inode = VFS_I(cip);
  375. error = xfs_inode_init_security(inode, dir, &dentry->d_name);
  376. if (unlikely(error))
  377. goto out_cleanup_inode;
  378. xfs_setup_iops(cip);
  379. d_instantiate(dentry, inode);
  380. xfs_finish_inode_setup(cip);
  381. return 0;
  382. out_cleanup_inode:
  383. xfs_finish_inode_setup(cip);
  384. xfs_cleanup_inode(dir, inode, dentry);
  385. xfs_irele(cip);
  386. out:
  387. return error;
  388. }
  389. STATIC int
  390. xfs_vn_rename(
  391. struct user_namespace *mnt_userns,
  392. struct inode *odir,
  393. struct dentry *odentry,
  394. struct inode *ndir,
  395. struct dentry *ndentry,
  396. unsigned int flags)
  397. {
  398. struct inode *new_inode = d_inode(ndentry);
  399. int omode = 0;
  400. int error;
  401. struct xfs_name oname;
  402. struct xfs_name nname;
  403. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  404. return -EINVAL;
  405. /* if we are exchanging files, we need to set i_mode of both files */
  406. if (flags & RENAME_EXCHANGE)
  407. omode = d_inode(ndentry)->i_mode;
  408. error = xfs_dentry_mode_to_name(&oname, odentry, omode);
  409. if (omode && unlikely(error))
  410. return error;
  411. error = xfs_dentry_mode_to_name(&nname, ndentry,
  412. d_inode(odentry)->i_mode);
  413. if (unlikely(error))
  414. return error;
  415. return xfs_rename(mnt_userns, XFS_I(odir), &oname,
  416. XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
  417. new_inode ? XFS_I(new_inode) : NULL, flags);
  418. }
  419. /*
  420. * careful here - this function can get called recursively, so
  421. * we need to be very careful about how much stack we use.
  422. * uio is kmalloced for this reason...
  423. */
  424. STATIC const char *
  425. xfs_vn_get_link(
  426. struct dentry *dentry,
  427. struct inode *inode,
  428. struct delayed_call *done)
  429. {
  430. char *link;
  431. int error = -ENOMEM;
  432. if (!dentry)
  433. return ERR_PTR(-ECHILD);
  434. link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
  435. if (!link)
  436. goto out_err;
  437. error = xfs_readlink(XFS_I(d_inode(dentry)), link);
  438. if (unlikely(error))
  439. goto out_kfree;
  440. set_delayed_call(done, kfree_link, link);
  441. return link;
  442. out_kfree:
  443. kfree(link);
  444. out_err:
  445. return ERR_PTR(error);
  446. }
  447. static uint32_t
  448. xfs_stat_blksize(
  449. struct xfs_inode *ip)
  450. {
  451. struct xfs_mount *mp = ip->i_mount;
  452. /*
  453. * If the file blocks are being allocated from a realtime volume, then
  454. * always return the realtime extent size.
  455. */
  456. if (XFS_IS_REALTIME_INODE(ip))
  457. return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip));
  458. /*
  459. * Allow large block sizes to be reported to userspace programs if the
  460. * "largeio" mount option is used.
  461. *
  462. * If compatibility mode is specified, simply return the basic unit of
  463. * caching so that we don't get inefficient read/modify/write I/O from
  464. * user apps. Otherwise....
  465. *
  466. * If the underlying volume is a stripe, then return the stripe width in
  467. * bytes as the recommended I/O size. It is not a stripe and we've set a
  468. * default buffered I/O size, return that, otherwise return the compat
  469. * default.
  470. */
  471. if (xfs_has_large_iosize(mp)) {
  472. if (mp->m_swidth)
  473. return XFS_FSB_TO_B(mp, mp->m_swidth);
  474. if (xfs_has_allocsize(mp))
  475. return 1U << mp->m_allocsize_log;
  476. }
  477. return PAGE_SIZE;
  478. }
  479. STATIC int
  480. xfs_vn_getattr(
  481. struct user_namespace *mnt_userns,
  482. const struct path *path,
  483. struct kstat *stat,
  484. u32 request_mask,
  485. unsigned int query_flags)
  486. {
  487. struct inode *inode = d_inode(path->dentry);
  488. struct xfs_inode *ip = XFS_I(inode);
  489. struct xfs_mount *mp = ip->i_mount;
  490. vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
  491. vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
  492. trace_xfs_getattr(ip);
  493. if (xfs_is_shutdown(mp))
  494. return -EIO;
  495. stat->size = XFS_ISIZE(ip);
  496. stat->dev = inode->i_sb->s_dev;
  497. stat->mode = inode->i_mode;
  498. stat->nlink = inode->i_nlink;
  499. stat->uid = vfsuid_into_kuid(vfsuid);
  500. stat->gid = vfsgid_into_kgid(vfsgid);
  501. stat->ino = ip->i_ino;
  502. stat->atime = inode->i_atime;
  503. stat->mtime = inode->i_mtime;
  504. stat->ctime = inode->i_ctime;
  505. stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
  506. if (xfs_has_v3inodes(mp)) {
  507. if (request_mask & STATX_BTIME) {
  508. stat->result_mask |= STATX_BTIME;
  509. stat->btime = ip->i_crtime;
  510. }
  511. }
  512. /*
  513. * Note: If you add another clause to set an attribute flag, please
  514. * update attributes_mask below.
  515. */
  516. if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
  517. stat->attributes |= STATX_ATTR_IMMUTABLE;
  518. if (ip->i_diflags & XFS_DIFLAG_APPEND)
  519. stat->attributes |= STATX_ATTR_APPEND;
  520. if (ip->i_diflags & XFS_DIFLAG_NODUMP)
  521. stat->attributes |= STATX_ATTR_NODUMP;
  522. stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
  523. STATX_ATTR_APPEND |
  524. STATX_ATTR_NODUMP);
  525. switch (inode->i_mode & S_IFMT) {
  526. case S_IFBLK:
  527. case S_IFCHR:
  528. stat->blksize = BLKDEV_IOSIZE;
  529. stat->rdev = inode->i_rdev;
  530. break;
  531. case S_IFREG:
  532. if (request_mask & STATX_DIOALIGN) {
  533. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  534. struct block_device *bdev = target->bt_bdev;
  535. stat->result_mask |= STATX_DIOALIGN;
  536. stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
  537. stat->dio_offset_align = bdev_logical_block_size(bdev);
  538. }
  539. fallthrough;
  540. default:
  541. stat->blksize = xfs_stat_blksize(ip);
  542. stat->rdev = 0;
  543. break;
  544. }
  545. return 0;
  546. }
  547. static int
  548. xfs_vn_change_ok(
  549. struct user_namespace *mnt_userns,
  550. struct dentry *dentry,
  551. struct iattr *iattr)
  552. {
  553. struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
  554. if (xfs_is_readonly(mp))
  555. return -EROFS;
  556. if (xfs_is_shutdown(mp))
  557. return -EIO;
  558. return setattr_prepare(mnt_userns, dentry, iattr);
  559. }
  560. /*
  561. * Set non-size attributes of an inode.
  562. *
  563. * Caution: The caller of this function is responsible for calling
  564. * setattr_prepare() or otherwise verifying the change is fine.
  565. */
  566. static int
  567. xfs_setattr_nonsize(
  568. struct user_namespace *mnt_userns,
  569. struct xfs_inode *ip,
  570. struct iattr *iattr)
  571. {
  572. xfs_mount_t *mp = ip->i_mount;
  573. struct inode *inode = VFS_I(ip);
  574. int mask = iattr->ia_valid;
  575. xfs_trans_t *tp;
  576. int error;
  577. kuid_t uid = GLOBAL_ROOT_UID;
  578. kgid_t gid = GLOBAL_ROOT_GID;
  579. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  580. struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL;
  581. ASSERT((mask & ATTR_SIZE) == 0);
  582. /*
  583. * If disk quotas is on, we make sure that the dquots do exist on disk,
  584. * before we start any other transactions. Trying to do this later
  585. * is messy. We don't care to take a readlock to look at the ids
  586. * in inode here, because we can't hold it across the trans_reserve.
  587. * If the IDs do change before we take the ilock, we're covered
  588. * because the i_*dquot fields will get updated anyway.
  589. */
  590. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  591. uint qflags = 0;
  592. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  593. uid = from_vfsuid(mnt_userns, i_user_ns(inode),
  594. iattr->ia_vfsuid);
  595. qflags |= XFS_QMOPT_UQUOTA;
  596. } else {
  597. uid = inode->i_uid;
  598. }
  599. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  600. gid = from_vfsgid(mnt_userns, i_user_ns(inode),
  601. iattr->ia_vfsgid);
  602. qflags |= XFS_QMOPT_GQUOTA;
  603. } else {
  604. gid = inode->i_gid;
  605. }
  606. /*
  607. * We take a reference when we initialize udqp and gdqp,
  608. * so it is important that we never blindly double trip on
  609. * the same variable. See xfs_create() for an example.
  610. */
  611. ASSERT(udqp == NULL);
  612. ASSERT(gdqp == NULL);
  613. error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
  614. qflags, &udqp, &gdqp, NULL);
  615. if (error)
  616. return error;
  617. }
  618. error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
  619. has_capability_noaudit(current, CAP_FOWNER), &tp);
  620. if (error)
  621. goto out_dqrele;
  622. /*
  623. * Register quota modifications in the transaction. Must be the owner
  624. * or privileged. These IDs could have changed since we last looked at
  625. * them. But, we're assured that if the ownership did change while we
  626. * didn't have the inode locked, inode's dquot(s) would have changed
  627. * also.
  628. */
  629. if (XFS_IS_UQUOTA_ON(mp) &&
  630. i_uid_needs_update(mnt_userns, iattr, inode)) {
  631. ASSERT(udqp);
  632. old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp);
  633. }
  634. if (XFS_IS_GQUOTA_ON(mp) &&
  635. i_gid_needs_update(mnt_userns, iattr, inode)) {
  636. ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp));
  637. ASSERT(gdqp);
  638. old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp);
  639. }
  640. setattr_copy(mnt_userns, inode, iattr);
  641. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  642. XFS_STATS_INC(mp, xs_ig_attrchg);
  643. if (xfs_has_wsync(mp))
  644. xfs_trans_set_sync(tp);
  645. error = xfs_trans_commit(tp);
  646. /*
  647. * Release any dquot(s) the inode had kept before chown.
  648. */
  649. xfs_qm_dqrele(old_udqp);
  650. xfs_qm_dqrele(old_gdqp);
  651. xfs_qm_dqrele(udqp);
  652. xfs_qm_dqrele(gdqp);
  653. if (error)
  654. return error;
  655. /*
  656. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  657. * update. We could avoid this with linked transactions
  658. * and passing down the transaction pointer all the way
  659. * to attr_set. No previous user of the generic
  660. * Posix ACL code seems to care about this issue either.
  661. */
  662. if (mask & ATTR_MODE) {
  663. error = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
  664. if (error)
  665. return error;
  666. }
  667. return 0;
  668. out_dqrele:
  669. xfs_qm_dqrele(udqp);
  670. xfs_qm_dqrele(gdqp);
  671. return error;
  672. }
  673. /*
  674. * Truncate file. Must have write permission and not be a directory.
  675. *
  676. * Caution: The caller of this function is responsible for calling
  677. * setattr_prepare() or otherwise verifying the change is fine.
  678. */
  679. STATIC int
  680. xfs_setattr_size(
  681. struct user_namespace *mnt_userns,
  682. struct xfs_inode *ip,
  683. struct iattr *iattr)
  684. {
  685. struct xfs_mount *mp = ip->i_mount;
  686. struct inode *inode = VFS_I(ip);
  687. xfs_off_t oldsize, newsize;
  688. struct xfs_trans *tp;
  689. int error;
  690. uint lock_flags = 0;
  691. bool did_zeroing = false;
  692. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  693. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  694. ASSERT(S_ISREG(inode->i_mode));
  695. ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  696. ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
  697. oldsize = inode->i_size;
  698. newsize = iattr->ia_size;
  699. /*
  700. * Short circuit the truncate case for zero length files.
  701. */
  702. if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
  703. if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
  704. return 0;
  705. /*
  706. * Use the regular setattr path to update the timestamps.
  707. */
  708. iattr->ia_valid &= ~ATTR_SIZE;
  709. return xfs_setattr_nonsize(mnt_userns, ip, iattr);
  710. }
  711. /*
  712. * Make sure that the dquots are attached to the inode.
  713. */
  714. error = xfs_qm_dqattach(ip);
  715. if (error)
  716. return error;
  717. /*
  718. * Wait for all direct I/O to complete.
  719. */
  720. inode_dio_wait(inode);
  721. /*
  722. * File data changes must be complete before we start the transaction to
  723. * modify the inode. This needs to be done before joining the inode to
  724. * the transaction because the inode cannot be unlocked once it is a
  725. * part of the transaction.
  726. *
  727. * Start with zeroing any data beyond EOF that we may expose on file
  728. * extension, or zeroing out the rest of the block on a downward
  729. * truncate.
  730. */
  731. if (newsize > oldsize) {
  732. trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
  733. error = xfs_zero_range(ip, oldsize, newsize - oldsize,
  734. &did_zeroing);
  735. } else {
  736. /*
  737. * iomap won't detect a dirty page over an unwritten block (or a
  738. * cow block over a hole) and subsequently skips zeroing the
  739. * newly post-EOF portion of the page. Flush the new EOF to
  740. * convert the block before the pagecache truncate.
  741. */
  742. error = filemap_write_and_wait_range(inode->i_mapping, newsize,
  743. newsize);
  744. if (error)
  745. return error;
  746. error = xfs_truncate_page(ip, newsize, &did_zeroing);
  747. }
  748. if (error)
  749. return error;
  750. /*
  751. * We've already locked out new page faults, so now we can safely remove
  752. * pages from the page cache knowing they won't get refaulted until we
  753. * drop the XFS_MMAP_EXCL lock after the extent manipulations are
  754. * complete. The truncate_setsize() call also cleans partial EOF page
  755. * PTEs on extending truncates and hence ensures sub-page block size
  756. * filesystems are correctly handled, too.
  757. *
  758. * We have to do all the page cache truncate work outside the
  759. * transaction context as the "lock" order is page lock->log space
  760. * reservation as defined by extent allocation in the writeback path.
  761. * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
  762. * having already truncated the in-memory version of the file (i.e. made
  763. * user visible changes). There's not much we can do about this, except
  764. * to hope that the caller sees ENOMEM and retries the truncate
  765. * operation.
  766. *
  767. * And we update in-core i_size and truncate page cache beyond newsize
  768. * before writeback the [i_disk_size, newsize] range, so we're
  769. * guaranteed not to write stale data past the new EOF on truncate down.
  770. */
  771. truncate_setsize(inode, newsize);
  772. /*
  773. * We are going to log the inode size change in this transaction so
  774. * any previous writes that are beyond the on disk EOF and the new
  775. * EOF that have not been written out need to be written here. If we
  776. * do not write the data out, we expose ourselves to the null files
  777. * problem. Note that this includes any block zeroing we did above;
  778. * otherwise those blocks may not be zeroed after a crash.
  779. */
  780. if (did_zeroing ||
  781. (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
  782. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  783. ip->i_disk_size, newsize - 1);
  784. if (error)
  785. return error;
  786. }
  787. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  788. if (error)
  789. return error;
  790. lock_flags |= XFS_ILOCK_EXCL;
  791. xfs_ilock(ip, XFS_ILOCK_EXCL);
  792. xfs_trans_ijoin(tp, ip, 0);
  793. /*
  794. * Only change the c/mtime if we are changing the size or we are
  795. * explicitly asked to change it. This handles the semantic difference
  796. * between truncate() and ftruncate() as implemented in the VFS.
  797. *
  798. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  799. * special case where we need to update the times despite not having
  800. * these flags set. For all other operations the VFS set these flags
  801. * explicitly if it wants a timestamp update.
  802. */
  803. if (newsize != oldsize &&
  804. !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
  805. iattr->ia_ctime = iattr->ia_mtime =
  806. current_time(inode);
  807. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  808. }
  809. /*
  810. * The first thing we do is set the size to new_size permanently on
  811. * disk. This way we don't have to worry about anyone ever being able
  812. * to look at the data being freed even in the face of a crash.
  813. * What we're getting around here is the case where we free a block, it
  814. * is allocated to another file, it is written to, and then we crash.
  815. * If the new data gets written to the file but the log buffers
  816. * containing the free and reallocation don't, then we'd end up with
  817. * garbage in the blocks being freed. As long as we make the new size
  818. * permanent before actually freeing any blocks it doesn't matter if
  819. * they get written to.
  820. */
  821. ip->i_disk_size = newsize;
  822. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  823. if (newsize <= oldsize) {
  824. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  825. if (error)
  826. goto out_trans_cancel;
  827. /*
  828. * Truncated "down", so we're removing references to old data
  829. * here - if we delay flushing for a long time, we expose
  830. * ourselves unduly to the notorious NULL files problem. So,
  831. * we mark this inode and flush it when the file is closed,
  832. * and do not wait the usual (long) time for writeout.
  833. */
  834. xfs_iflags_set(ip, XFS_ITRUNCATED);
  835. /* A truncate down always removes post-EOF blocks. */
  836. xfs_inode_clear_eofblocks_tag(ip);
  837. }
  838. ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
  839. setattr_copy(mnt_userns, inode, iattr);
  840. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  841. XFS_STATS_INC(mp, xs_ig_attrchg);
  842. if (xfs_has_wsync(mp))
  843. xfs_trans_set_sync(tp);
  844. error = xfs_trans_commit(tp);
  845. out_unlock:
  846. if (lock_flags)
  847. xfs_iunlock(ip, lock_flags);
  848. return error;
  849. out_trans_cancel:
  850. xfs_trans_cancel(tp);
  851. goto out_unlock;
  852. }
  853. int
  854. xfs_vn_setattr_size(
  855. struct user_namespace *mnt_userns,
  856. struct dentry *dentry,
  857. struct iattr *iattr)
  858. {
  859. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  860. int error;
  861. trace_xfs_setattr(ip);
  862. error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
  863. if (error)
  864. return error;
  865. return xfs_setattr_size(mnt_userns, ip, iattr);
  866. }
  867. STATIC int
  868. xfs_vn_setattr(
  869. struct user_namespace *mnt_userns,
  870. struct dentry *dentry,
  871. struct iattr *iattr)
  872. {
  873. struct inode *inode = d_inode(dentry);
  874. struct xfs_inode *ip = XFS_I(inode);
  875. int error;
  876. if (iattr->ia_valid & ATTR_SIZE) {
  877. uint iolock;
  878. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  879. iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  880. error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
  881. if (error) {
  882. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  883. return error;
  884. }
  885. error = xfs_vn_setattr_size(mnt_userns, dentry, iattr);
  886. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  887. } else {
  888. trace_xfs_setattr(ip);
  889. error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
  890. if (!error)
  891. error = xfs_setattr_nonsize(mnt_userns, ip, iattr);
  892. }
  893. return error;
  894. }
  895. STATIC int
  896. xfs_vn_update_time(
  897. struct inode *inode,
  898. struct timespec64 *now,
  899. int flags)
  900. {
  901. struct xfs_inode *ip = XFS_I(inode);
  902. struct xfs_mount *mp = ip->i_mount;
  903. int log_flags = XFS_ILOG_TIMESTAMP;
  904. struct xfs_trans *tp;
  905. int error;
  906. trace_xfs_update_time(ip);
  907. if (inode->i_sb->s_flags & SB_LAZYTIME) {
  908. if (!((flags & S_VERSION) &&
  909. inode_maybe_inc_iversion(inode, false)))
  910. return generic_update_time(inode, now, flags);
  911. /* Capture the iversion update that just occurred */
  912. log_flags |= XFS_ILOG_CORE;
  913. }
  914. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  915. if (error)
  916. return error;
  917. xfs_ilock(ip, XFS_ILOCK_EXCL);
  918. if (flags & S_CTIME)
  919. inode->i_ctime = *now;
  920. if (flags & S_MTIME)
  921. inode->i_mtime = *now;
  922. if (flags & S_ATIME)
  923. inode->i_atime = *now;
  924. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  925. xfs_trans_log_inode(tp, ip, log_flags);
  926. return xfs_trans_commit(tp);
  927. }
  928. STATIC int
  929. xfs_vn_fiemap(
  930. struct inode *inode,
  931. struct fiemap_extent_info *fieinfo,
  932. u64 start,
  933. u64 length)
  934. {
  935. int error;
  936. xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
  937. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  938. fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
  939. error = iomap_fiemap(inode, fieinfo, start, length,
  940. &xfs_xattr_iomap_ops);
  941. } else {
  942. error = iomap_fiemap(inode, fieinfo, start, length,
  943. &xfs_read_iomap_ops);
  944. }
  945. xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
  946. return error;
  947. }
  948. STATIC int
  949. xfs_vn_tmpfile(
  950. struct user_namespace *mnt_userns,
  951. struct inode *dir,
  952. struct file *file,
  953. umode_t mode)
  954. {
  955. int err = xfs_generic_create(mnt_userns, dir, file->f_path.dentry, mode, 0, file);
  956. return finish_open_simple(file, err);
  957. }
  958. static const struct inode_operations xfs_inode_operations = {
  959. .get_acl = xfs_get_acl,
  960. .set_acl = xfs_set_acl,
  961. .getattr = xfs_vn_getattr,
  962. .setattr = xfs_vn_setattr,
  963. .listxattr = xfs_vn_listxattr,
  964. .fiemap = xfs_vn_fiemap,
  965. .update_time = xfs_vn_update_time,
  966. .fileattr_get = xfs_fileattr_get,
  967. .fileattr_set = xfs_fileattr_set,
  968. };
  969. static const struct inode_operations xfs_dir_inode_operations = {
  970. .create = xfs_vn_create,
  971. .lookup = xfs_vn_lookup,
  972. .link = xfs_vn_link,
  973. .unlink = xfs_vn_unlink,
  974. .symlink = xfs_vn_symlink,
  975. .mkdir = xfs_vn_mkdir,
  976. /*
  977. * Yes, XFS uses the same method for rmdir and unlink.
  978. *
  979. * There are some subtile differences deeper in the code,
  980. * but we use S_ISDIR to check for those.
  981. */
  982. .rmdir = xfs_vn_unlink,
  983. .mknod = xfs_vn_mknod,
  984. .rename = xfs_vn_rename,
  985. .get_acl = xfs_get_acl,
  986. .set_acl = xfs_set_acl,
  987. .getattr = xfs_vn_getattr,
  988. .setattr = xfs_vn_setattr,
  989. .listxattr = xfs_vn_listxattr,
  990. .update_time = xfs_vn_update_time,
  991. .tmpfile = xfs_vn_tmpfile,
  992. .fileattr_get = xfs_fileattr_get,
  993. .fileattr_set = xfs_fileattr_set,
  994. };
  995. static const struct inode_operations xfs_dir_ci_inode_operations = {
  996. .create = xfs_vn_create,
  997. .lookup = xfs_vn_ci_lookup,
  998. .link = xfs_vn_link,
  999. .unlink = xfs_vn_unlink,
  1000. .symlink = xfs_vn_symlink,
  1001. .mkdir = xfs_vn_mkdir,
  1002. /*
  1003. * Yes, XFS uses the same method for rmdir and unlink.
  1004. *
  1005. * There are some subtile differences deeper in the code,
  1006. * but we use S_ISDIR to check for those.
  1007. */
  1008. .rmdir = xfs_vn_unlink,
  1009. .mknod = xfs_vn_mknod,
  1010. .rename = xfs_vn_rename,
  1011. .get_acl = xfs_get_acl,
  1012. .set_acl = xfs_set_acl,
  1013. .getattr = xfs_vn_getattr,
  1014. .setattr = xfs_vn_setattr,
  1015. .listxattr = xfs_vn_listxattr,
  1016. .update_time = xfs_vn_update_time,
  1017. .tmpfile = xfs_vn_tmpfile,
  1018. .fileattr_get = xfs_fileattr_get,
  1019. .fileattr_set = xfs_fileattr_set,
  1020. };
  1021. static const struct inode_operations xfs_symlink_inode_operations = {
  1022. .get_link = xfs_vn_get_link,
  1023. .getattr = xfs_vn_getattr,
  1024. .setattr = xfs_vn_setattr,
  1025. .listxattr = xfs_vn_listxattr,
  1026. .update_time = xfs_vn_update_time,
  1027. };
  1028. /* Figure out if this file actually supports DAX. */
  1029. static bool
  1030. xfs_inode_supports_dax(
  1031. struct xfs_inode *ip)
  1032. {
  1033. struct xfs_mount *mp = ip->i_mount;
  1034. /* Only supported on regular files. */
  1035. if (!S_ISREG(VFS_I(ip)->i_mode))
  1036. return false;
  1037. /* Only supported on non-reflinked files. */
  1038. if (xfs_is_reflink_inode(ip))
  1039. return false;
  1040. /* Block size must match page size */
  1041. if (mp->m_sb.sb_blocksize != PAGE_SIZE)
  1042. return false;
  1043. /* Device has to support DAX too. */
  1044. return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
  1045. }
  1046. static bool
  1047. xfs_inode_should_enable_dax(
  1048. struct xfs_inode *ip)
  1049. {
  1050. if (!IS_ENABLED(CONFIG_FS_DAX))
  1051. return false;
  1052. if (xfs_has_dax_never(ip->i_mount))
  1053. return false;
  1054. if (!xfs_inode_supports_dax(ip))
  1055. return false;
  1056. if (xfs_has_dax_always(ip->i_mount))
  1057. return true;
  1058. if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
  1059. return true;
  1060. return false;
  1061. }
  1062. void
  1063. xfs_diflags_to_iflags(
  1064. struct xfs_inode *ip,
  1065. bool init)
  1066. {
  1067. struct inode *inode = VFS_I(ip);
  1068. unsigned int xflags = xfs_ip2xflags(ip);
  1069. unsigned int flags = 0;
  1070. ASSERT(!(IS_DAX(inode) && init));
  1071. if (xflags & FS_XFLAG_IMMUTABLE)
  1072. flags |= S_IMMUTABLE;
  1073. if (xflags & FS_XFLAG_APPEND)
  1074. flags |= S_APPEND;
  1075. if (xflags & FS_XFLAG_SYNC)
  1076. flags |= S_SYNC;
  1077. if (xflags & FS_XFLAG_NOATIME)
  1078. flags |= S_NOATIME;
  1079. if (init && xfs_inode_should_enable_dax(ip))
  1080. flags |= S_DAX;
  1081. /*
  1082. * S_DAX can only be set during inode initialization and is never set by
  1083. * the VFS, so we cannot mask off S_DAX in i_flags.
  1084. */
  1085. inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
  1086. inode->i_flags |= flags;
  1087. }
  1088. /*
  1089. * Initialize the Linux inode.
  1090. *
  1091. * When reading existing inodes from disk this is called directly from xfs_iget,
  1092. * when creating a new inode it is called from xfs_init_new_inode after setting
  1093. * up the inode. These callers have different criteria for clearing XFS_INEW, so
  1094. * leave it up to the caller to deal with unlocking the inode appropriately.
  1095. */
  1096. void
  1097. xfs_setup_inode(
  1098. struct xfs_inode *ip)
  1099. {
  1100. struct inode *inode = &ip->i_vnode;
  1101. gfp_t gfp_mask;
  1102. inode->i_ino = ip->i_ino;
  1103. inode->i_state |= I_NEW;
  1104. inode_sb_list_add(inode);
  1105. /* make the inode look hashed for the writeback code */
  1106. inode_fake_hash(inode);
  1107. i_size_write(inode, ip->i_disk_size);
  1108. xfs_diflags_to_iflags(ip, true);
  1109. if (S_ISDIR(inode->i_mode)) {
  1110. /*
  1111. * We set the i_rwsem class here to avoid potential races with
  1112. * lockdep_annotate_inode_mutex_key() reinitialising the lock
  1113. * after a filehandle lookup has already found the inode in
  1114. * cache before it has been unlocked via unlock_new_inode().
  1115. */
  1116. lockdep_set_class(&inode->i_rwsem,
  1117. &inode->i_sb->s_type->i_mutex_dir_key);
  1118. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
  1119. } else {
  1120. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
  1121. }
  1122. /*
  1123. * Ensure all page cache allocations are done from GFP_NOFS context to
  1124. * prevent direct reclaim recursion back into the filesystem and blowing
  1125. * stacks or deadlocking.
  1126. */
  1127. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  1128. mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
  1129. /*
  1130. * If there is no attribute fork no ACL can exist on this inode,
  1131. * and it can't have any file capabilities attached to it either.
  1132. */
  1133. if (!xfs_inode_has_attr_fork(ip)) {
  1134. inode_has_no_xattr(inode);
  1135. cache_no_acl(inode);
  1136. }
  1137. }
  1138. void
  1139. xfs_setup_iops(
  1140. struct xfs_inode *ip)
  1141. {
  1142. struct inode *inode = &ip->i_vnode;
  1143. switch (inode->i_mode & S_IFMT) {
  1144. case S_IFREG:
  1145. inode->i_op = &xfs_inode_operations;
  1146. inode->i_fop = &xfs_file_operations;
  1147. if (IS_DAX(inode))
  1148. inode->i_mapping->a_ops = &xfs_dax_aops;
  1149. else
  1150. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1151. break;
  1152. case S_IFDIR:
  1153. if (xfs_has_asciici(XFS_M(inode->i_sb)))
  1154. inode->i_op = &xfs_dir_ci_inode_operations;
  1155. else
  1156. inode->i_op = &xfs_dir_inode_operations;
  1157. inode->i_fop = &xfs_dir_file_operations;
  1158. break;
  1159. case S_IFLNK:
  1160. inode->i_op = &xfs_symlink_inode_operations;
  1161. break;
  1162. default:
  1163. inode->i_op = &xfs_inode_operations;
  1164. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1165. break;
  1166. }
  1167. }