xfs_attr.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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_defer.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_attr_sf.h"
  17. #include "xfs_inode.h"
  18. #include "xfs_trans.h"
  19. #include "xfs_bmap.h"
  20. #include "xfs_bmap_btree.h"
  21. #include "xfs_attr.h"
  22. #include "xfs_attr_leaf.h"
  23. #include "xfs_attr_remote.h"
  24. #include "xfs_quota.h"
  25. #include "xfs_trans_space.h"
  26. #include "xfs_trace.h"
  27. #include "xfs_attr_item.h"
  28. #include "xfs_xattr.h"
  29. struct kmem_cache *xfs_attr_intent_cache;
  30. /*
  31. * xfs_attr.c
  32. *
  33. * Provide the external interfaces to manage attribute lists.
  34. */
  35. /*========================================================================
  36. * Function prototypes for the kernel.
  37. *========================================================================*/
  38. /*
  39. * Internal routines when attribute list fits inside the inode.
  40. */
  41. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  42. /*
  43. * Internal routines when attribute list is one block.
  44. */
  45. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  46. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  47. STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
  48. STATIC int xfs_attr_leaf_try_add(struct xfs_da_args *args);
  49. /*
  50. * Internal routines when attribute list is more than one block.
  51. */
  52. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  53. STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
  54. static int xfs_attr_node_try_addname(struct xfs_attr_intent *attr);
  55. STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr);
  56. STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr);
  57. STATIC int xfs_attr_node_lookup(struct xfs_da_args *args,
  58. struct xfs_da_state *state);
  59. int
  60. xfs_inode_hasattr(
  61. struct xfs_inode *ip)
  62. {
  63. if (!xfs_inode_has_attr_fork(ip))
  64. return 0;
  65. if (ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
  66. ip->i_af.if_nextents == 0)
  67. return 0;
  68. return 1;
  69. }
  70. /*
  71. * Returns true if the there is exactly only block in the attr fork, in which
  72. * case the attribute fork consists of a single leaf block entry.
  73. */
  74. bool
  75. xfs_attr_is_leaf(
  76. struct xfs_inode *ip)
  77. {
  78. struct xfs_ifork *ifp = &ip->i_af;
  79. struct xfs_iext_cursor icur;
  80. struct xfs_bmbt_irec imap;
  81. if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
  82. return false;
  83. xfs_iext_first(ifp, &icur);
  84. xfs_iext_get_extent(ifp, &icur, &imap);
  85. return imap.br_startoff == 0 && imap.br_blockcount == 1;
  86. }
  87. /*
  88. * XXX (dchinner): name path state saving and refilling is an optimisation to
  89. * avoid needing to look up name entries after rolling transactions removing
  90. * remote xattr blocks between the name entry lookup and name entry removal.
  91. * This optimisation got sidelined when combining the set and remove state
  92. * machines, but the code has been left in place because it is worthwhile to
  93. * restore the optimisation once the combined state machine paths have settled.
  94. *
  95. * This comment is a public service announcement to remind Future Dave that he
  96. * still needs to restore this code to working order.
  97. */
  98. #if 0
  99. /*
  100. * Fill in the disk block numbers in the state structure for the buffers
  101. * that are attached to the state structure.
  102. * This is done so that we can quickly reattach ourselves to those buffers
  103. * after some set of transaction commits have released these buffers.
  104. */
  105. static int
  106. xfs_attr_fillstate(xfs_da_state_t *state)
  107. {
  108. xfs_da_state_path_t *path;
  109. xfs_da_state_blk_t *blk;
  110. int level;
  111. trace_xfs_attr_fillstate(state->args);
  112. /*
  113. * Roll down the "path" in the state structure, storing the on-disk
  114. * block number for those buffers in the "path".
  115. */
  116. path = &state->path;
  117. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  118. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  119. if (blk->bp) {
  120. blk->disk_blkno = xfs_buf_daddr(blk->bp);
  121. blk->bp = NULL;
  122. } else {
  123. blk->disk_blkno = 0;
  124. }
  125. }
  126. /*
  127. * Roll down the "altpath" in the state structure, storing the on-disk
  128. * block number for those buffers in the "altpath".
  129. */
  130. path = &state->altpath;
  131. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  132. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  133. if (blk->bp) {
  134. blk->disk_blkno = xfs_buf_daddr(blk->bp);
  135. blk->bp = NULL;
  136. } else {
  137. blk->disk_blkno = 0;
  138. }
  139. }
  140. return 0;
  141. }
  142. /*
  143. * Reattach the buffers to the state structure based on the disk block
  144. * numbers stored in the state structure.
  145. * This is done after some set of transaction commits have released those
  146. * buffers from our grip.
  147. */
  148. static int
  149. xfs_attr_refillstate(xfs_da_state_t *state)
  150. {
  151. xfs_da_state_path_t *path;
  152. xfs_da_state_blk_t *blk;
  153. int level, error;
  154. trace_xfs_attr_refillstate(state->args);
  155. /*
  156. * Roll down the "path" in the state structure, storing the on-disk
  157. * block number for those buffers in the "path".
  158. */
  159. path = &state->path;
  160. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  161. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  162. if (blk->disk_blkno) {
  163. error = xfs_da3_node_read_mapped(state->args->trans,
  164. state->args->dp, blk->disk_blkno,
  165. &blk->bp, XFS_ATTR_FORK);
  166. if (error)
  167. return error;
  168. } else {
  169. blk->bp = NULL;
  170. }
  171. }
  172. /*
  173. * Roll down the "altpath" in the state structure, storing the on-disk
  174. * block number for those buffers in the "altpath".
  175. */
  176. path = &state->altpath;
  177. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  178. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  179. if (blk->disk_blkno) {
  180. error = xfs_da3_node_read_mapped(state->args->trans,
  181. state->args->dp, blk->disk_blkno,
  182. &blk->bp, XFS_ATTR_FORK);
  183. if (error)
  184. return error;
  185. } else {
  186. blk->bp = NULL;
  187. }
  188. }
  189. return 0;
  190. }
  191. #else
  192. static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; }
  193. #endif
  194. /*========================================================================
  195. * Overall external interface routines.
  196. *========================================================================*/
  197. /*
  198. * Retrieve an extended attribute and its value. Must have ilock.
  199. * Returns 0 on successful retrieval, otherwise an error.
  200. */
  201. int
  202. xfs_attr_get_ilocked(
  203. struct xfs_da_args *args)
  204. {
  205. ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  206. if (!xfs_inode_hasattr(args->dp))
  207. return -ENOATTR;
  208. if (args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
  209. return xfs_attr_shortform_getvalue(args);
  210. if (xfs_attr_is_leaf(args->dp))
  211. return xfs_attr_leaf_get(args);
  212. return xfs_attr_node_get(args);
  213. }
  214. /*
  215. * Retrieve an extended attribute by name, and its value if requested.
  216. *
  217. * If args->valuelen is zero, then the caller does not want the value, just an
  218. * indication whether the attribute exists and the size of the value if it
  219. * exists. The size is returned in args.valuelen.
  220. *
  221. * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
  222. * for the value after existence of the attribute has been determined. The
  223. * caller always has to free args->value if it is set, no matter if this
  224. * function was successful or not.
  225. *
  226. * If the attribute is found, but exceeds the size limit set by the caller in
  227. * args->valuelen, return -ERANGE with the size of the attribute that was found
  228. * in args->valuelen.
  229. */
  230. int
  231. xfs_attr_get(
  232. struct xfs_da_args *args)
  233. {
  234. uint lock_mode;
  235. int error;
  236. XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
  237. if (xfs_is_shutdown(args->dp->i_mount))
  238. return -EIO;
  239. args->geo = args->dp->i_mount->m_attr_geo;
  240. args->whichfork = XFS_ATTR_FORK;
  241. args->hashval = xfs_da_hashname(args->name, args->namelen);
  242. /* Entirely possible to look up a name which doesn't exist */
  243. args->op_flags = XFS_DA_OP_OKNOENT;
  244. lock_mode = xfs_ilock_attr_map_shared(args->dp);
  245. error = xfs_attr_get_ilocked(args);
  246. xfs_iunlock(args->dp, lock_mode);
  247. return error;
  248. }
  249. /*
  250. * Calculate how many blocks we need for the new attribute,
  251. */
  252. int
  253. xfs_attr_calc_size(
  254. struct xfs_da_args *args,
  255. int *local)
  256. {
  257. struct xfs_mount *mp = args->dp->i_mount;
  258. int size;
  259. int nblks;
  260. /*
  261. * Determine space new attribute will use, and if it would be
  262. * "local" or "remote" (note: local != inline).
  263. */
  264. size = xfs_attr_leaf_newentsize(args, local);
  265. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  266. if (*local) {
  267. if (size > (args->geo->blksize / 2)) {
  268. /* Double split possible */
  269. nblks *= 2;
  270. }
  271. } else {
  272. /*
  273. * Out of line attribute, cannot double split, but
  274. * make room for the attribute value itself.
  275. */
  276. uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
  277. nblks += dblocks;
  278. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  279. }
  280. return nblks;
  281. }
  282. /* Initialize transaction reservation for attr operations */
  283. void
  284. xfs_init_attr_trans(
  285. struct xfs_da_args *args,
  286. struct xfs_trans_res *tres,
  287. unsigned int *total)
  288. {
  289. struct xfs_mount *mp = args->dp->i_mount;
  290. if (args->value) {
  291. tres->tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
  292. M_RES(mp)->tr_attrsetrt.tr_logres *
  293. args->total;
  294. tres->tr_logcount = XFS_ATTRSET_LOG_COUNT;
  295. tres->tr_logflags = XFS_TRANS_PERM_LOG_RES;
  296. *total = args->total;
  297. } else {
  298. *tres = M_RES(mp)->tr_attrrm;
  299. *total = XFS_ATTRRM_SPACE_RES(mp);
  300. }
  301. }
  302. /*
  303. * Add an attr to a shortform fork. If there is no space,
  304. * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC.
  305. * to use.
  306. */
  307. STATIC int
  308. xfs_attr_try_sf_addname(
  309. struct xfs_inode *dp,
  310. struct xfs_da_args *args)
  311. {
  312. int error;
  313. /*
  314. * Build initial attribute list (if required).
  315. */
  316. if (dp->i_af.if_format == XFS_DINODE_FMT_EXTENTS)
  317. xfs_attr_shortform_create(args);
  318. error = xfs_attr_shortform_addname(args);
  319. if (error == -ENOSPC)
  320. return error;
  321. /*
  322. * Commit the shortform mods, and we're done.
  323. * NOTE: this is also the error path (EEXIST, etc).
  324. */
  325. if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
  326. xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
  327. if (xfs_has_wsync(dp->i_mount))
  328. xfs_trans_set_sync(args->trans);
  329. return error;
  330. }
  331. static int
  332. xfs_attr_sf_addname(
  333. struct xfs_attr_intent *attr)
  334. {
  335. struct xfs_da_args *args = attr->xattri_da_args;
  336. struct xfs_inode *dp = args->dp;
  337. int error = 0;
  338. error = xfs_attr_try_sf_addname(dp, args);
  339. if (error != -ENOSPC) {
  340. ASSERT(!error || error == -EEXIST);
  341. attr->xattri_dela_state = XFS_DAS_DONE;
  342. goto out;
  343. }
  344. /*
  345. * It won't fit in the shortform, transform to a leaf block. GROT:
  346. * another possible req'mt for a double-split btree op.
  347. */
  348. error = xfs_attr_shortform_to_leaf(args);
  349. if (error)
  350. return error;
  351. attr->xattri_dela_state = XFS_DAS_LEAF_ADD;
  352. out:
  353. trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp);
  354. return error;
  355. }
  356. /*
  357. * Handle the state change on completion of a multi-state attr operation.
  358. *
  359. * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first
  360. * modification in a attr replace operation and we still have to do the second
  361. * state, indicated by @replace_state.
  362. *
  363. * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on
  364. * completion of the second half of the attr replace operation we correctly
  365. * signal that it is done.
  366. */
  367. static enum xfs_delattr_state
  368. xfs_attr_complete_op(
  369. struct xfs_attr_intent *attr,
  370. enum xfs_delattr_state replace_state)
  371. {
  372. struct xfs_da_args *args = attr->xattri_da_args;
  373. bool do_replace = args->op_flags & XFS_DA_OP_REPLACE;
  374. args->op_flags &= ~XFS_DA_OP_REPLACE;
  375. if (do_replace) {
  376. args->attr_filter &= ~XFS_ATTR_INCOMPLETE;
  377. return replace_state;
  378. }
  379. return XFS_DAS_DONE;
  380. }
  381. static int
  382. xfs_attr_leaf_addname(
  383. struct xfs_attr_intent *attr)
  384. {
  385. struct xfs_da_args *args = attr->xattri_da_args;
  386. int error;
  387. ASSERT(xfs_attr_is_leaf(args->dp));
  388. /*
  389. * Use the leaf buffer we may already hold locked as a result of
  390. * a sf-to-leaf conversion.
  391. */
  392. error = xfs_attr_leaf_try_add(args);
  393. if (error == -ENOSPC) {
  394. error = xfs_attr3_leaf_to_node(args);
  395. if (error)
  396. return error;
  397. /*
  398. * We're not in leaf format anymore, so roll the transaction and
  399. * retry the add to the newly allocated node block.
  400. */
  401. attr->xattri_dela_state = XFS_DAS_NODE_ADD;
  402. goto out;
  403. }
  404. if (error)
  405. return error;
  406. /*
  407. * We need to commit and roll if we need to allocate remote xattr blocks
  408. * or perform more xattr manipulations. Otherwise there is nothing more
  409. * to do and we can return success.
  410. */
  411. if (args->rmtblkno)
  412. attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT;
  413. else
  414. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  415. XFS_DAS_LEAF_REPLACE);
  416. out:
  417. trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
  418. return error;
  419. }
  420. /*
  421. * Add an entry to a node format attr tree.
  422. *
  423. * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell
  424. * the difference between leaf + remote attr blocks and a node format tree,
  425. * so we may still end up having to convert from leaf to node format here.
  426. */
  427. static int
  428. xfs_attr_node_addname(
  429. struct xfs_attr_intent *attr)
  430. {
  431. struct xfs_da_args *args = attr->xattri_da_args;
  432. int error;
  433. error = xfs_attr_node_addname_find_attr(attr);
  434. if (error)
  435. return error;
  436. error = xfs_attr_node_try_addname(attr);
  437. if (error == -ENOSPC) {
  438. error = xfs_attr3_leaf_to_node(args);
  439. if (error)
  440. return error;
  441. /*
  442. * No state change, we really are in node form now
  443. * but we need the transaction rolled to continue.
  444. */
  445. goto out;
  446. }
  447. if (error)
  448. return error;
  449. if (args->rmtblkno)
  450. attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT;
  451. else
  452. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  453. XFS_DAS_NODE_REPLACE);
  454. out:
  455. trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp);
  456. return error;
  457. }
  458. static int
  459. xfs_attr_rmtval_alloc(
  460. struct xfs_attr_intent *attr)
  461. {
  462. struct xfs_da_args *args = attr->xattri_da_args;
  463. int error = 0;
  464. /*
  465. * If there was an out-of-line value, allocate the blocks we
  466. * identified for its storage and copy the value. This is done
  467. * after we create the attribute so that we don't overflow the
  468. * maximum size of a transaction and/or hit a deadlock.
  469. */
  470. if (attr->xattri_blkcnt > 0) {
  471. error = xfs_attr_rmtval_set_blk(attr);
  472. if (error)
  473. return error;
  474. /* Roll the transaction only if there is more to allocate. */
  475. if (attr->xattri_blkcnt > 0)
  476. goto out;
  477. }
  478. error = xfs_attr_rmtval_set_value(args);
  479. if (error)
  480. return error;
  481. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  482. ++attr->xattri_dela_state);
  483. /*
  484. * If we are not doing a rename, we've finished the operation but still
  485. * have to clear the incomplete flag protecting the new attr from
  486. * exposing partially initialised state if we crash during creation.
  487. */
  488. if (attr->xattri_dela_state == XFS_DAS_DONE)
  489. error = xfs_attr3_leaf_clearflag(args);
  490. out:
  491. trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp);
  492. return error;
  493. }
  494. /*
  495. * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
  496. * for later deletion of the entry.
  497. */
  498. static int
  499. xfs_attr_leaf_mark_incomplete(
  500. struct xfs_da_args *args,
  501. struct xfs_da_state *state)
  502. {
  503. int error;
  504. /*
  505. * Fill in disk block numbers in the state structure
  506. * so that we can get the buffers back after we commit
  507. * several transactions in the following calls.
  508. */
  509. error = xfs_attr_fillstate(state);
  510. if (error)
  511. return error;
  512. /*
  513. * Mark the attribute as INCOMPLETE
  514. */
  515. return xfs_attr3_leaf_setflag(args);
  516. }
  517. /* Ensure the da state of an xattr deferred work item is ready to go. */
  518. static inline void
  519. xfs_attr_item_init_da_state(
  520. struct xfs_attr_intent *attr)
  521. {
  522. struct xfs_da_args *args = attr->xattri_da_args;
  523. if (!attr->xattri_da_state)
  524. attr->xattri_da_state = xfs_da_state_alloc(args);
  525. else
  526. xfs_da_state_reset(attr->xattri_da_state, args);
  527. }
  528. /*
  529. * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
  530. * the blocks are valid. Attr keys with remote blocks will be marked
  531. * incomplete.
  532. */
  533. static
  534. int xfs_attr_node_removename_setup(
  535. struct xfs_attr_intent *attr)
  536. {
  537. struct xfs_da_args *args = attr->xattri_da_args;
  538. struct xfs_da_state *state;
  539. int error;
  540. xfs_attr_item_init_da_state(attr);
  541. error = xfs_attr_node_lookup(args, attr->xattri_da_state);
  542. if (error != -EEXIST)
  543. goto out;
  544. error = 0;
  545. state = attr->xattri_da_state;
  546. ASSERT(state->path.blk[state->path.active - 1].bp != NULL);
  547. ASSERT(state->path.blk[state->path.active - 1].magic ==
  548. XFS_ATTR_LEAF_MAGIC);
  549. error = xfs_attr_leaf_mark_incomplete(args, state);
  550. if (error)
  551. goto out;
  552. if (args->rmtblkno > 0)
  553. error = xfs_attr_rmtval_invalidate(args);
  554. out:
  555. if (error) {
  556. xfs_da_state_free(attr->xattri_da_state);
  557. attr->xattri_da_state = NULL;
  558. }
  559. return error;
  560. }
  561. /*
  562. * Remove the original attr we have just replaced. This is dependent on the
  563. * original lookup and insert placing the old attr in args->blkno/args->index
  564. * and the new attr in args->blkno2/args->index2.
  565. */
  566. static int
  567. xfs_attr_leaf_remove_attr(
  568. struct xfs_attr_intent *attr)
  569. {
  570. struct xfs_da_args *args = attr->xattri_da_args;
  571. struct xfs_inode *dp = args->dp;
  572. struct xfs_buf *bp = NULL;
  573. int forkoff;
  574. int error;
  575. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  576. &bp);
  577. if (error)
  578. return error;
  579. xfs_attr3_leaf_remove(bp, args);
  580. forkoff = xfs_attr_shortform_allfit(bp, dp);
  581. if (forkoff)
  582. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  583. /* bp is gone due to xfs_da_shrink_inode */
  584. return error;
  585. }
  586. /*
  587. * Shrink an attribute from leaf to shortform. Used by the node format remove
  588. * path when the node format collapses to a single block and so we have to check
  589. * if it can be collapsed further.
  590. */
  591. static int
  592. xfs_attr_leaf_shrink(
  593. struct xfs_da_args *args)
  594. {
  595. struct xfs_inode *dp = args->dp;
  596. struct xfs_buf *bp;
  597. int forkoff;
  598. int error;
  599. if (!xfs_attr_is_leaf(dp))
  600. return 0;
  601. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
  602. if (error)
  603. return error;
  604. forkoff = xfs_attr_shortform_allfit(bp, dp);
  605. if (forkoff) {
  606. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  607. /* bp is gone due to xfs_da_shrink_inode */
  608. } else {
  609. xfs_trans_brelse(args->trans, bp);
  610. }
  611. return error;
  612. }
  613. /*
  614. * Run the attribute operation specified in @attr.
  615. *
  616. * This routine is meant to function as a delayed operation and will set the
  617. * state to XFS_DAS_DONE when the operation is complete. Calling functions will
  618. * need to handle this, and recall the function until either an error or
  619. * XFS_DAS_DONE is detected.
  620. */
  621. int
  622. xfs_attr_set_iter(
  623. struct xfs_attr_intent *attr)
  624. {
  625. struct xfs_da_args *args = attr->xattri_da_args;
  626. int error = 0;
  627. /* State machine switch */
  628. next_state:
  629. switch (attr->xattri_dela_state) {
  630. case XFS_DAS_UNINIT:
  631. ASSERT(0);
  632. return -EFSCORRUPTED;
  633. case XFS_DAS_SF_ADD:
  634. return xfs_attr_sf_addname(attr);
  635. case XFS_DAS_LEAF_ADD:
  636. return xfs_attr_leaf_addname(attr);
  637. case XFS_DAS_NODE_ADD:
  638. return xfs_attr_node_addname(attr);
  639. case XFS_DAS_SF_REMOVE:
  640. error = xfs_attr_sf_removename(args);
  641. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  642. xfs_attr_init_add_state(args));
  643. break;
  644. case XFS_DAS_LEAF_REMOVE:
  645. error = xfs_attr_leaf_removename(args);
  646. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  647. xfs_attr_init_add_state(args));
  648. break;
  649. case XFS_DAS_NODE_REMOVE:
  650. error = xfs_attr_node_removename_setup(attr);
  651. if (error == -ENOATTR &&
  652. (args->op_flags & XFS_DA_OP_RECOVERY)) {
  653. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  654. xfs_attr_init_add_state(args));
  655. error = 0;
  656. break;
  657. }
  658. if (error)
  659. return error;
  660. attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT;
  661. if (args->rmtblkno == 0)
  662. attr->xattri_dela_state++;
  663. break;
  664. case XFS_DAS_LEAF_SET_RMT:
  665. case XFS_DAS_NODE_SET_RMT:
  666. error = xfs_attr_rmtval_find_space(attr);
  667. if (error)
  668. return error;
  669. attr->xattri_dela_state++;
  670. fallthrough;
  671. case XFS_DAS_LEAF_ALLOC_RMT:
  672. case XFS_DAS_NODE_ALLOC_RMT:
  673. error = xfs_attr_rmtval_alloc(attr);
  674. if (error)
  675. return error;
  676. if (attr->xattri_dela_state == XFS_DAS_DONE)
  677. break;
  678. goto next_state;
  679. case XFS_DAS_LEAF_REPLACE:
  680. case XFS_DAS_NODE_REPLACE:
  681. /*
  682. * We must "flip" the incomplete flags on the "new" and "old"
  683. * attribute/value pairs so that one disappears and one appears
  684. * atomically.
  685. */
  686. error = xfs_attr3_leaf_flipflags(args);
  687. if (error)
  688. return error;
  689. /*
  690. * We must commit the flag value change now to make it atomic
  691. * and then we can start the next trans in series at REMOVE_OLD.
  692. */
  693. attr->xattri_dela_state++;
  694. break;
  695. case XFS_DAS_LEAF_REMOVE_OLD:
  696. case XFS_DAS_NODE_REMOVE_OLD:
  697. /*
  698. * If we have a remote attr, start the process of removing it
  699. * by invalidating any cached buffers.
  700. *
  701. * If we don't have a remote attr, we skip the remote block
  702. * removal state altogether with a second state increment.
  703. */
  704. xfs_attr_restore_rmt_blk(args);
  705. if (args->rmtblkno) {
  706. error = xfs_attr_rmtval_invalidate(args);
  707. if (error)
  708. return error;
  709. } else {
  710. attr->xattri_dela_state++;
  711. }
  712. attr->xattri_dela_state++;
  713. goto next_state;
  714. case XFS_DAS_LEAF_REMOVE_RMT:
  715. case XFS_DAS_NODE_REMOVE_RMT:
  716. error = xfs_attr_rmtval_remove(attr);
  717. if (error == -EAGAIN) {
  718. error = 0;
  719. break;
  720. }
  721. if (error)
  722. return error;
  723. /*
  724. * We've finished removing the remote attr blocks, so commit the
  725. * transaction and move on to removing the attr name from the
  726. * leaf/node block. Removing the attr might require a full
  727. * transaction reservation for btree block freeing, so we
  728. * can't do that in the same transaction where we removed the
  729. * remote attr blocks.
  730. */
  731. attr->xattri_dela_state++;
  732. break;
  733. case XFS_DAS_LEAF_REMOVE_ATTR:
  734. error = xfs_attr_leaf_remove_attr(attr);
  735. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  736. xfs_attr_init_add_state(args));
  737. break;
  738. case XFS_DAS_NODE_REMOVE_ATTR:
  739. error = xfs_attr_node_remove_attr(attr);
  740. if (!error)
  741. error = xfs_attr_leaf_shrink(args);
  742. attr->xattri_dela_state = xfs_attr_complete_op(attr,
  743. xfs_attr_init_add_state(args));
  744. break;
  745. default:
  746. ASSERT(0);
  747. break;
  748. }
  749. trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp);
  750. return error;
  751. }
  752. /*
  753. * Return EEXIST if attr is found, or ENOATTR if not
  754. */
  755. static int
  756. xfs_attr_lookup(
  757. struct xfs_da_args *args)
  758. {
  759. struct xfs_inode *dp = args->dp;
  760. struct xfs_buf *bp = NULL;
  761. struct xfs_da_state *state;
  762. int error;
  763. if (!xfs_inode_hasattr(dp))
  764. return -ENOATTR;
  765. if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
  766. return xfs_attr_sf_findname(args, NULL, NULL);
  767. if (xfs_attr_is_leaf(dp)) {
  768. error = xfs_attr_leaf_hasname(args, &bp);
  769. if (bp)
  770. xfs_trans_brelse(args->trans, bp);
  771. return error;
  772. }
  773. state = xfs_da_state_alloc(args);
  774. error = xfs_attr_node_lookup(args, state);
  775. xfs_da_state_free(state);
  776. return error;
  777. }
  778. static int
  779. xfs_attr_intent_init(
  780. struct xfs_da_args *args,
  781. unsigned int op_flags, /* op flag (set or remove) */
  782. struct xfs_attr_intent **attr) /* new xfs_attr_intent */
  783. {
  784. struct xfs_attr_intent *new;
  785. new = kmem_cache_zalloc(xfs_attr_intent_cache, GFP_NOFS | __GFP_NOFAIL);
  786. new->xattri_op_flags = op_flags;
  787. new->xattri_da_args = args;
  788. *attr = new;
  789. return 0;
  790. }
  791. /* Sets an attribute for an inode as a deferred operation */
  792. static int
  793. xfs_attr_defer_add(
  794. struct xfs_da_args *args)
  795. {
  796. struct xfs_attr_intent *new;
  797. int error = 0;
  798. error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_SET, &new);
  799. if (error)
  800. return error;
  801. new->xattri_dela_state = xfs_attr_init_add_state(args);
  802. xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
  803. trace_xfs_attr_defer_add(new->xattri_dela_state, args->dp);
  804. return 0;
  805. }
  806. /* Sets an attribute for an inode as a deferred operation */
  807. static int
  808. xfs_attr_defer_replace(
  809. struct xfs_da_args *args)
  810. {
  811. struct xfs_attr_intent *new;
  812. int error = 0;
  813. error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_REPLACE, &new);
  814. if (error)
  815. return error;
  816. new->xattri_dela_state = xfs_attr_init_replace_state(args);
  817. xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
  818. trace_xfs_attr_defer_replace(new->xattri_dela_state, args->dp);
  819. return 0;
  820. }
  821. /* Removes an attribute for an inode as a deferred operation */
  822. static int
  823. xfs_attr_defer_remove(
  824. struct xfs_da_args *args)
  825. {
  826. struct xfs_attr_intent *new;
  827. int error;
  828. error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_REMOVE, &new);
  829. if (error)
  830. return error;
  831. new->xattri_dela_state = xfs_attr_init_remove_state(args);
  832. xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
  833. trace_xfs_attr_defer_remove(new->xattri_dela_state, args->dp);
  834. return 0;
  835. }
  836. /*
  837. * Note: If args->value is NULL the attribute will be removed, just like the
  838. * Linux ->setattr API.
  839. */
  840. int
  841. xfs_attr_set(
  842. struct xfs_da_args *args)
  843. {
  844. struct xfs_inode *dp = args->dp;
  845. struct xfs_mount *mp = dp->i_mount;
  846. struct xfs_trans_res tres;
  847. bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
  848. int error, local;
  849. int rmt_blks = 0;
  850. unsigned int total;
  851. if (xfs_is_shutdown(dp->i_mount))
  852. return -EIO;
  853. error = xfs_qm_dqattach(dp);
  854. if (error)
  855. return error;
  856. args->geo = mp->m_attr_geo;
  857. args->whichfork = XFS_ATTR_FORK;
  858. args->hashval = xfs_da_hashname(args->name, args->namelen);
  859. /*
  860. * We have no control over the attribute names that userspace passes us
  861. * to remove, so we have to allow the name lookup prior to attribute
  862. * removal to fail as well. Preserve the logged flag, since we need
  863. * to pass that through to the logging code.
  864. */
  865. args->op_flags = XFS_DA_OP_OKNOENT |
  866. (args->op_flags & XFS_DA_OP_LOGGED);
  867. if (args->value) {
  868. XFS_STATS_INC(mp, xs_attr_set);
  869. args->total = xfs_attr_calc_size(args, &local);
  870. /*
  871. * If the inode doesn't have an attribute fork, add one.
  872. * (inode must not be locked when we call this routine)
  873. */
  874. if (xfs_inode_has_attr_fork(dp) == 0) {
  875. int sf_size = sizeof(struct xfs_attr_sf_hdr) +
  876. xfs_attr_sf_entsize_byname(args->namelen,
  877. args->valuelen);
  878. error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
  879. if (error)
  880. return error;
  881. }
  882. if (!local)
  883. rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
  884. } else {
  885. XFS_STATS_INC(mp, xs_attr_remove);
  886. rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
  887. }
  888. /*
  889. * Root fork attributes can use reserved data blocks for this
  890. * operation if necessary
  891. */
  892. xfs_init_attr_trans(args, &tres, &total);
  893. error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
  894. if (error)
  895. return error;
  896. if (args->value || xfs_inode_hasattr(dp)) {
  897. error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
  898. XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
  899. if (error == -EFBIG)
  900. error = xfs_iext_count_upgrade(args->trans, dp,
  901. XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
  902. if (error)
  903. goto out_trans_cancel;
  904. }
  905. error = xfs_attr_lookup(args);
  906. switch (error) {
  907. case -EEXIST:
  908. /* if no value, we are performing a remove operation */
  909. if (!args->value) {
  910. error = xfs_attr_defer_remove(args);
  911. break;
  912. }
  913. /* Pure create fails if the attr already exists */
  914. if (args->attr_flags & XATTR_CREATE)
  915. goto out_trans_cancel;
  916. error = xfs_attr_defer_replace(args);
  917. break;
  918. case -ENOATTR:
  919. /* Can't remove what isn't there. */
  920. if (!args->value)
  921. goto out_trans_cancel;
  922. /* Pure replace fails if no existing attr to replace. */
  923. if (args->attr_flags & XATTR_REPLACE)
  924. goto out_trans_cancel;
  925. error = xfs_attr_defer_add(args);
  926. break;
  927. default:
  928. goto out_trans_cancel;
  929. }
  930. if (error)
  931. goto out_trans_cancel;
  932. /*
  933. * If this is a synchronous mount, make sure that the
  934. * transaction goes to disk before returning to the user.
  935. */
  936. if (xfs_has_wsync(mp))
  937. xfs_trans_set_sync(args->trans);
  938. if (!(args->op_flags & XFS_DA_OP_NOTIME))
  939. xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
  940. /*
  941. * Commit the last in the sequence of transactions.
  942. */
  943. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  944. error = xfs_trans_commit(args->trans);
  945. out_unlock:
  946. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  947. return error;
  948. out_trans_cancel:
  949. if (args->trans)
  950. xfs_trans_cancel(args->trans);
  951. goto out_unlock;
  952. }
  953. /*========================================================================
  954. * External routines when attribute list is inside the inode
  955. *========================================================================*/
  956. static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
  957. {
  958. struct xfs_attr_shortform *sf;
  959. sf = (struct xfs_attr_shortform *)dp->i_af.if_u1.if_data;
  960. return be16_to_cpu(sf->hdr.totsize);
  961. }
  962. /*
  963. * Add a name to the shortform attribute list structure
  964. * This is the external routine.
  965. */
  966. static int
  967. xfs_attr_shortform_addname(
  968. struct xfs_da_args *args)
  969. {
  970. int newsize, forkoff;
  971. int error;
  972. trace_xfs_attr_sf_addname(args);
  973. error = xfs_attr_shortform_lookup(args);
  974. switch (error) {
  975. case -ENOATTR:
  976. if (args->op_flags & XFS_DA_OP_REPLACE)
  977. return error;
  978. break;
  979. case -EEXIST:
  980. if (!(args->op_flags & XFS_DA_OP_REPLACE))
  981. return error;
  982. error = xfs_attr_sf_removename(args);
  983. if (error)
  984. return error;
  985. /*
  986. * Since we have removed the old attr, clear XFS_DA_OP_REPLACE
  987. * so that the new attr doesn't fit in shortform format, the
  988. * leaf format add routine won't trip over the attr not being
  989. * around.
  990. */
  991. args->op_flags &= ~XFS_DA_OP_REPLACE;
  992. break;
  993. case 0:
  994. break;
  995. default:
  996. return error;
  997. }
  998. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  999. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  1000. return -ENOSPC;
  1001. newsize = xfs_attr_sf_totsize(args->dp);
  1002. newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
  1003. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  1004. if (!forkoff)
  1005. return -ENOSPC;
  1006. xfs_attr_shortform_add(args, forkoff);
  1007. return 0;
  1008. }
  1009. /*========================================================================
  1010. * External routines when attribute list is one block
  1011. *========================================================================*/
  1012. /* Save the current remote block info and clear the current pointers. */
  1013. static void
  1014. xfs_attr_save_rmt_blk(
  1015. struct xfs_da_args *args)
  1016. {
  1017. args->blkno2 = args->blkno;
  1018. args->index2 = args->index;
  1019. args->rmtblkno2 = args->rmtblkno;
  1020. args->rmtblkcnt2 = args->rmtblkcnt;
  1021. args->rmtvaluelen2 = args->rmtvaluelen;
  1022. args->rmtblkno = 0;
  1023. args->rmtblkcnt = 0;
  1024. args->rmtvaluelen = 0;
  1025. }
  1026. /* Set stored info about a remote block */
  1027. static void
  1028. xfs_attr_restore_rmt_blk(
  1029. struct xfs_da_args *args)
  1030. {
  1031. args->blkno = args->blkno2;
  1032. args->index = args->index2;
  1033. args->rmtblkno = args->rmtblkno2;
  1034. args->rmtblkcnt = args->rmtblkcnt2;
  1035. args->rmtvaluelen = args->rmtvaluelen2;
  1036. }
  1037. /*
  1038. * Tries to add an attribute to an inode in leaf form
  1039. *
  1040. * This function is meant to execute as part of a delayed operation and leaves
  1041. * the transaction handling to the caller. On success the attribute is added
  1042. * and the inode and transaction are left dirty. If there is not enough space,
  1043. * the attr data is converted to node format and -ENOSPC is returned. Caller is
  1044. * responsible for handling the dirty inode and transaction or adding the attr
  1045. * in node format.
  1046. */
  1047. STATIC int
  1048. xfs_attr_leaf_try_add(
  1049. struct xfs_da_args *args)
  1050. {
  1051. struct xfs_buf *bp;
  1052. int error;
  1053. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
  1054. if (error)
  1055. return error;
  1056. /*
  1057. * Look up the xattr name to set the insertion point for the new xattr.
  1058. */
  1059. error = xfs_attr3_leaf_lookup_int(bp, args);
  1060. switch (error) {
  1061. case -ENOATTR:
  1062. if (args->op_flags & XFS_DA_OP_REPLACE)
  1063. goto out_brelse;
  1064. break;
  1065. case -EEXIST:
  1066. if (!(args->op_flags & XFS_DA_OP_REPLACE))
  1067. goto out_brelse;
  1068. trace_xfs_attr_leaf_replace(args);
  1069. /*
  1070. * Save the existing remote attr state so that the current
  1071. * values reflect the state of the new attribute we are about to
  1072. * add, not the attribute we just found and will remove later.
  1073. */
  1074. xfs_attr_save_rmt_blk(args);
  1075. break;
  1076. case 0:
  1077. break;
  1078. default:
  1079. goto out_brelse;
  1080. }
  1081. return xfs_attr3_leaf_add(bp, args);
  1082. out_brelse:
  1083. xfs_trans_brelse(args->trans, bp);
  1084. return error;
  1085. }
  1086. /*
  1087. * Return EEXIST if attr is found, or ENOATTR if not
  1088. */
  1089. STATIC int
  1090. xfs_attr_leaf_hasname(
  1091. struct xfs_da_args *args,
  1092. struct xfs_buf **bp)
  1093. {
  1094. int error = 0;
  1095. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
  1096. if (error)
  1097. return error;
  1098. error = xfs_attr3_leaf_lookup_int(*bp, args);
  1099. if (error != -ENOATTR && error != -EEXIST)
  1100. xfs_trans_brelse(args->trans, *bp);
  1101. return error;
  1102. }
  1103. /*
  1104. * Remove a name from the leaf attribute list structure
  1105. *
  1106. * This leaf block cannot have a "remote" value, we only call this routine
  1107. * if bmap_one_block() says there is only one block (ie: no remote blks).
  1108. */
  1109. STATIC int
  1110. xfs_attr_leaf_removename(
  1111. struct xfs_da_args *args)
  1112. {
  1113. struct xfs_inode *dp;
  1114. struct xfs_buf *bp;
  1115. int error, forkoff;
  1116. trace_xfs_attr_leaf_removename(args);
  1117. /*
  1118. * Remove the attribute.
  1119. */
  1120. dp = args->dp;
  1121. error = xfs_attr_leaf_hasname(args, &bp);
  1122. if (error == -ENOATTR) {
  1123. xfs_trans_brelse(args->trans, bp);
  1124. if (args->op_flags & XFS_DA_OP_RECOVERY)
  1125. return 0;
  1126. return error;
  1127. } else if (error != -EEXIST)
  1128. return error;
  1129. xfs_attr3_leaf_remove(bp, args);
  1130. /*
  1131. * If the result is small enough, shrink it all into the inode.
  1132. */
  1133. forkoff = xfs_attr_shortform_allfit(bp, dp);
  1134. if (forkoff)
  1135. return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1136. /* bp is gone due to xfs_da_shrink_inode */
  1137. return 0;
  1138. }
  1139. /*
  1140. * Look up a name in a leaf attribute list structure.
  1141. *
  1142. * This leaf block cannot have a "remote" value, we only call this routine
  1143. * if bmap_one_block() says there is only one block (ie: no remote blks).
  1144. *
  1145. * Returns 0 on successful retrieval, otherwise an error.
  1146. */
  1147. STATIC int
  1148. xfs_attr_leaf_get(xfs_da_args_t *args)
  1149. {
  1150. struct xfs_buf *bp;
  1151. int error;
  1152. trace_xfs_attr_leaf_get(args);
  1153. error = xfs_attr_leaf_hasname(args, &bp);
  1154. if (error == -ENOATTR) {
  1155. xfs_trans_brelse(args->trans, bp);
  1156. return error;
  1157. } else if (error != -EEXIST)
  1158. return error;
  1159. error = xfs_attr3_leaf_getvalue(bp, args);
  1160. xfs_trans_brelse(args->trans, bp);
  1161. return error;
  1162. }
  1163. /* Return EEXIST if attr is found, or ENOATTR if not. */
  1164. STATIC int
  1165. xfs_attr_node_lookup(
  1166. struct xfs_da_args *args,
  1167. struct xfs_da_state *state)
  1168. {
  1169. int retval, error;
  1170. /*
  1171. * Search to see if name exists, and get back a pointer to it.
  1172. */
  1173. error = xfs_da3_node_lookup_int(state, &retval);
  1174. if (error)
  1175. return error;
  1176. return retval;
  1177. }
  1178. /*========================================================================
  1179. * External routines when attribute list size > geo->blksize
  1180. *========================================================================*/
  1181. STATIC int
  1182. xfs_attr_node_addname_find_attr(
  1183. struct xfs_attr_intent *attr)
  1184. {
  1185. struct xfs_da_args *args = attr->xattri_da_args;
  1186. int error;
  1187. /*
  1188. * Search to see if name already exists, and get back a pointer
  1189. * to where it should go.
  1190. */
  1191. xfs_attr_item_init_da_state(attr);
  1192. error = xfs_attr_node_lookup(args, attr->xattri_da_state);
  1193. switch (error) {
  1194. case -ENOATTR:
  1195. if (args->op_flags & XFS_DA_OP_REPLACE)
  1196. goto error;
  1197. break;
  1198. case -EEXIST:
  1199. if (!(args->op_flags & XFS_DA_OP_REPLACE))
  1200. goto error;
  1201. trace_xfs_attr_node_replace(args);
  1202. /*
  1203. * Save the existing remote attr state so that the current
  1204. * values reflect the state of the new attribute we are about to
  1205. * add, not the attribute we just found and will remove later.
  1206. */
  1207. xfs_attr_save_rmt_blk(args);
  1208. break;
  1209. case 0:
  1210. break;
  1211. default:
  1212. goto error;
  1213. }
  1214. return 0;
  1215. error:
  1216. if (attr->xattri_da_state) {
  1217. xfs_da_state_free(attr->xattri_da_state);
  1218. attr->xattri_da_state = NULL;
  1219. }
  1220. return error;
  1221. }
  1222. /*
  1223. * Add a name to a Btree-format attribute list.
  1224. *
  1225. * This will involve walking down the Btree, and may involve splitting
  1226. * leaf nodes and even splitting intermediate nodes up to and including
  1227. * the root node (a special case of an intermediate node).
  1228. */
  1229. static int
  1230. xfs_attr_node_try_addname(
  1231. struct xfs_attr_intent *attr)
  1232. {
  1233. struct xfs_da_state *state = attr->xattri_da_state;
  1234. struct xfs_da_state_blk *blk;
  1235. int error;
  1236. trace_xfs_attr_node_addname(state->args);
  1237. blk = &state->path.blk[state->path.active-1];
  1238. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1239. error = xfs_attr3_leaf_add(blk->bp, state->args);
  1240. if (error == -ENOSPC) {
  1241. if (state->path.active == 1) {
  1242. /*
  1243. * Its really a single leaf node, but it had
  1244. * out-of-line values so it looked like it *might*
  1245. * have been a b-tree. Let the caller deal with this.
  1246. */
  1247. goto out;
  1248. }
  1249. /*
  1250. * Split as many Btree elements as required.
  1251. * This code tracks the new and old attr's location
  1252. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  1253. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  1254. */
  1255. error = xfs_da3_split(state);
  1256. if (error)
  1257. goto out;
  1258. } else {
  1259. /*
  1260. * Addition succeeded, update Btree hashvals.
  1261. */
  1262. xfs_da3_fixhashpath(state, &state->path);
  1263. }
  1264. out:
  1265. xfs_da_state_free(state);
  1266. attr->xattri_da_state = NULL;
  1267. return error;
  1268. }
  1269. static int
  1270. xfs_attr_node_removename(
  1271. struct xfs_da_args *args,
  1272. struct xfs_da_state *state)
  1273. {
  1274. struct xfs_da_state_blk *blk;
  1275. int retval;
  1276. /*
  1277. * Remove the name and update the hashvals in the tree.
  1278. */
  1279. blk = &state->path.blk[state->path.active-1];
  1280. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1281. retval = xfs_attr3_leaf_remove(blk->bp, args);
  1282. xfs_da3_fixhashpath(state, &state->path);
  1283. return retval;
  1284. }
  1285. static int
  1286. xfs_attr_node_remove_attr(
  1287. struct xfs_attr_intent *attr)
  1288. {
  1289. struct xfs_da_args *args = attr->xattri_da_args;
  1290. struct xfs_da_state *state = xfs_da_state_alloc(args);
  1291. int retval = 0;
  1292. int error = 0;
  1293. /*
  1294. * The attr we are removing has already been marked incomplete, so
  1295. * we need to set the filter appropriately to re-find the "old"
  1296. * attribute entry after any split ops.
  1297. */
  1298. args->attr_filter |= XFS_ATTR_INCOMPLETE;
  1299. error = xfs_da3_node_lookup_int(state, &retval);
  1300. if (error)
  1301. goto out;
  1302. error = xfs_attr_node_removename(args, state);
  1303. /*
  1304. * Check to see if the tree needs to be collapsed.
  1305. */
  1306. if (retval && (state->path.active > 1)) {
  1307. error = xfs_da3_join(state);
  1308. if (error)
  1309. goto out;
  1310. }
  1311. retval = error = 0;
  1312. out:
  1313. xfs_da_state_free(state);
  1314. if (error)
  1315. return error;
  1316. return retval;
  1317. }
  1318. /*
  1319. * Retrieve the attribute data from a node attribute list.
  1320. *
  1321. * This routine gets called for any attribute fork that has more than one
  1322. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1323. * "remote" values taking up more blocks.
  1324. *
  1325. * Returns 0 on successful retrieval, otherwise an error.
  1326. */
  1327. STATIC int
  1328. xfs_attr_node_get(
  1329. struct xfs_da_args *args)
  1330. {
  1331. struct xfs_da_state *state;
  1332. struct xfs_da_state_blk *blk;
  1333. int i;
  1334. int error;
  1335. trace_xfs_attr_node_get(args);
  1336. /*
  1337. * Search to see if name exists, and get back a pointer to it.
  1338. */
  1339. state = xfs_da_state_alloc(args);
  1340. error = xfs_attr_node_lookup(args, state);
  1341. if (error != -EEXIST)
  1342. goto out_release;
  1343. /*
  1344. * Get the value, local or "remote"
  1345. */
  1346. blk = &state->path.blk[state->path.active - 1];
  1347. error = xfs_attr3_leaf_getvalue(blk->bp, args);
  1348. /*
  1349. * If not in a transaction, we have to release all the buffers.
  1350. */
  1351. out_release:
  1352. for (i = 0; i < state->path.active; i++) {
  1353. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1354. state->path.blk[i].bp = NULL;
  1355. }
  1356. xfs_da_state_free(state);
  1357. return error;
  1358. }
  1359. /* Returns true if the attribute entry name is valid. */
  1360. bool
  1361. xfs_attr_namecheck(
  1362. const void *name,
  1363. size_t length)
  1364. {
  1365. /*
  1366. * MAXNAMELEN includes the trailing null, but (name/length) leave it
  1367. * out, so use >= for the length check.
  1368. */
  1369. if (length >= MAXNAMELEN)
  1370. return false;
  1371. /* There shouldn't be any nulls here */
  1372. return !memchr(name, 0, length);
  1373. }
  1374. int __init
  1375. xfs_attr_intent_init_cache(void)
  1376. {
  1377. xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent",
  1378. sizeof(struct xfs_attr_intent),
  1379. 0, 0, NULL);
  1380. return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM;
  1381. }
  1382. void
  1383. xfs_attr_intent_destroy_cache(void)
  1384. {
  1385. kmem_cache_destroy(xfs_attr_intent_cache);
  1386. xfs_attr_intent_cache = NULL;
  1387. }