xfs_attr.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_da_format.h"
  16. #include "xfs_da_btree.h"
  17. #include "xfs_attr_sf.h"
  18. #include "xfs_inode.h"
  19. #include "xfs_alloc.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_inode_item.h"
  22. #include "xfs_bmap.h"
  23. #include "xfs_bmap_util.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_attr.h"
  26. #include "xfs_attr_leaf.h"
  27. #include "xfs_attr_remote.h"
  28. #include "xfs_error.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_trans_space.h"
  31. #include "xfs_trace.h"
  32. /*
  33. * xfs_attr.c
  34. *
  35. * Provide the external interfaces to manage attribute lists.
  36. */
  37. /*========================================================================
  38. * Function prototypes for the kernel.
  39. *========================================================================*/
  40. /*
  41. * Internal routines when attribute list fits inside the inode.
  42. */
  43. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  44. /*
  45. * Internal routines when attribute list is one block.
  46. */
  47. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  48. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  49. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  50. /*
  51. * Internal routines when attribute list is more than one block.
  52. */
  53. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  54. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  55. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  56. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  57. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  58. STATIC int
  59. xfs_attr_args_init(
  60. struct xfs_da_args *args,
  61. struct xfs_inode *dp,
  62. const unsigned char *name,
  63. int flags)
  64. {
  65. if (!name)
  66. return -EINVAL;
  67. memset(args, 0, sizeof(*args));
  68. args->geo = dp->i_mount->m_attr_geo;
  69. args->whichfork = XFS_ATTR_FORK;
  70. args->dp = dp;
  71. args->flags = flags;
  72. args->name = name;
  73. args->namelen = strlen((const char *)name);
  74. if (args->namelen >= MAXNAMELEN)
  75. return -EFAULT; /* match IRIX behaviour */
  76. args->hashval = xfs_da_hashname(args->name, args->namelen);
  77. return 0;
  78. }
  79. int
  80. xfs_inode_hasattr(
  81. struct xfs_inode *ip)
  82. {
  83. if (!XFS_IFORK_Q(ip) ||
  84. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  85. ip->i_d.di_anextents == 0))
  86. return 0;
  87. return 1;
  88. }
  89. /*========================================================================
  90. * Overall external interface routines.
  91. *========================================================================*/
  92. /* Retrieve an extended attribute and its value. Must have ilock. */
  93. int
  94. xfs_attr_get_ilocked(
  95. struct xfs_inode *ip,
  96. struct xfs_da_args *args)
  97. {
  98. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  99. if (!xfs_inode_hasattr(ip))
  100. return -ENOATTR;
  101. else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
  102. return xfs_attr_shortform_getvalue(args);
  103. else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
  104. return xfs_attr_leaf_get(args);
  105. else
  106. return xfs_attr_node_get(args);
  107. }
  108. /* Retrieve an extended attribute by name, and its value. */
  109. int
  110. xfs_attr_get(
  111. struct xfs_inode *ip,
  112. const unsigned char *name,
  113. unsigned char *value,
  114. int *valuelenp,
  115. int flags)
  116. {
  117. struct xfs_da_args args;
  118. uint lock_mode;
  119. int error;
  120. XFS_STATS_INC(ip->i_mount, xs_attr_get);
  121. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  122. return -EIO;
  123. error = xfs_attr_args_init(&args, ip, name, flags);
  124. if (error)
  125. return error;
  126. args.value = value;
  127. args.valuelen = *valuelenp;
  128. /* Entirely possible to look up a name which doesn't exist */
  129. args.op_flags = XFS_DA_OP_OKNOENT;
  130. lock_mode = xfs_ilock_attr_map_shared(ip);
  131. error = xfs_attr_get_ilocked(ip, &args);
  132. xfs_iunlock(ip, lock_mode);
  133. *valuelenp = args.valuelen;
  134. return error == -EEXIST ? 0 : error;
  135. }
  136. /*
  137. * Calculate how many blocks we need for the new attribute,
  138. */
  139. STATIC int
  140. xfs_attr_calc_size(
  141. struct xfs_da_args *args,
  142. int *local)
  143. {
  144. struct xfs_mount *mp = args->dp->i_mount;
  145. int size;
  146. int nblks;
  147. /*
  148. * Determine space new attribute will use, and if it would be
  149. * "local" or "remote" (note: local != inline).
  150. */
  151. size = xfs_attr_leaf_newentsize(args, local);
  152. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  153. if (*local) {
  154. if (size > (args->geo->blksize / 2)) {
  155. /* Double split possible */
  156. nblks *= 2;
  157. }
  158. } else {
  159. /*
  160. * Out of line attribute, cannot double split, but
  161. * make room for the attribute value itself.
  162. */
  163. uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
  164. nblks += dblocks;
  165. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  166. }
  167. return nblks;
  168. }
  169. STATIC int
  170. xfs_attr_try_sf_addname(
  171. struct xfs_inode *dp,
  172. struct xfs_da_args *args)
  173. {
  174. struct xfs_mount *mp = dp->i_mount;
  175. int error, error2;
  176. error = xfs_attr_shortform_addname(args);
  177. if (error == -ENOSPC)
  178. return error;
  179. /*
  180. * Commit the shortform mods, and we're done.
  181. * NOTE: this is also the error path (EEXIST, etc).
  182. */
  183. if (!error && (args->flags & ATTR_KERNOTIME) == 0)
  184. xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
  185. if (mp->m_flags & XFS_MOUNT_WSYNC)
  186. xfs_trans_set_sync(args->trans);
  187. error2 = xfs_trans_commit(args->trans);
  188. args->trans = NULL;
  189. return error ? error : error2;
  190. }
  191. /*
  192. * Set the attribute specified in @args.
  193. */
  194. int
  195. xfs_attr_set_args(
  196. struct xfs_da_args *args)
  197. {
  198. struct xfs_inode *dp = args->dp;
  199. struct xfs_buf *leaf_bp = NULL;
  200. int error;
  201. /*
  202. * If the attribute list is non-existent or a shortform list,
  203. * upgrade it to a single-leaf-block attribute list.
  204. */
  205. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
  206. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  207. dp->i_d.di_anextents == 0)) {
  208. /*
  209. * Build initial attribute list (if required).
  210. */
  211. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  212. xfs_attr_shortform_create(args);
  213. /*
  214. * Try to add the attr to the attribute list in the inode.
  215. */
  216. error = xfs_attr_try_sf_addname(dp, args);
  217. if (error != -ENOSPC)
  218. return error;
  219. /*
  220. * It won't fit in the shortform, transform to a leaf block.
  221. * GROT: another possible req'mt for a double-split btree op.
  222. */
  223. error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
  224. if (error)
  225. return error;
  226. /*
  227. * Prevent the leaf buffer from being unlocked so that a
  228. * concurrent AIL push cannot grab the half-baked leaf
  229. * buffer and run into problems with the write verifier.
  230. * Once we're done rolling the transaction we can release
  231. * the hold and add the attr to the leaf.
  232. */
  233. xfs_trans_bhold(args->trans, leaf_bp);
  234. error = xfs_defer_finish(&args->trans);
  235. xfs_trans_bhold_release(args->trans, leaf_bp);
  236. if (error) {
  237. xfs_trans_brelse(args->trans, leaf_bp);
  238. return error;
  239. }
  240. }
  241. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
  242. error = xfs_attr_leaf_addname(args);
  243. else
  244. error = xfs_attr_node_addname(args);
  245. return error;
  246. }
  247. /*
  248. * Remove the attribute specified in @args.
  249. */
  250. int
  251. xfs_attr_remove_args(
  252. struct xfs_da_args *args)
  253. {
  254. struct xfs_inode *dp = args->dp;
  255. int error;
  256. if (!xfs_inode_hasattr(dp)) {
  257. error = -ENOATTR;
  258. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  259. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  260. error = xfs_attr_shortform_remove(args);
  261. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  262. error = xfs_attr_leaf_removename(args);
  263. } else {
  264. error = xfs_attr_node_removename(args);
  265. }
  266. return error;
  267. }
  268. int
  269. xfs_attr_set(
  270. struct xfs_inode *dp,
  271. const unsigned char *name,
  272. unsigned char *value,
  273. int valuelen,
  274. int flags)
  275. {
  276. struct xfs_mount *mp = dp->i_mount;
  277. struct xfs_da_args args;
  278. struct xfs_trans_res tres;
  279. int rsvd = (flags & ATTR_ROOT) != 0;
  280. int error, local;
  281. XFS_STATS_INC(mp, xs_attr_set);
  282. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  283. return -EIO;
  284. error = xfs_attr_args_init(&args, dp, name, flags);
  285. if (error)
  286. return error;
  287. args.value = value;
  288. args.valuelen = valuelen;
  289. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  290. args.total = xfs_attr_calc_size(&args, &local);
  291. error = xfs_qm_dqattach(dp);
  292. if (error)
  293. return error;
  294. /*
  295. * If the inode doesn't have an attribute fork, add one.
  296. * (inode must not be locked when we call this routine)
  297. */
  298. if (XFS_IFORK_Q(dp) == 0) {
  299. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  300. XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
  301. error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
  302. if (error)
  303. return error;
  304. }
  305. tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
  306. M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
  307. tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
  308. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  309. /*
  310. * Root fork attributes can use reserved data blocks for this
  311. * operation if necessary
  312. */
  313. error = xfs_trans_alloc(mp, &tres, args.total, 0,
  314. rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
  315. if (error)
  316. return error;
  317. xfs_ilock(dp, XFS_ILOCK_EXCL);
  318. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  319. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  320. XFS_QMOPT_RES_REGBLKS);
  321. if (error)
  322. goto out_trans_cancel;
  323. xfs_trans_ijoin(args.trans, dp, 0);
  324. error = xfs_attr_set_args(&args);
  325. if (error)
  326. goto out_trans_cancel;
  327. if (!args.trans) {
  328. /* shortform attribute has already been committed */
  329. goto out_unlock;
  330. }
  331. /*
  332. * If this is a synchronous mount, make sure that the
  333. * transaction goes to disk before returning to the user.
  334. */
  335. if (mp->m_flags & XFS_MOUNT_WSYNC)
  336. xfs_trans_set_sync(args.trans);
  337. if ((flags & ATTR_KERNOTIME) == 0)
  338. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  339. /*
  340. * Commit the last in the sequence of transactions.
  341. */
  342. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  343. error = xfs_trans_commit(args.trans);
  344. out_unlock:
  345. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  346. return error;
  347. out_trans_cancel:
  348. if (args.trans)
  349. xfs_trans_cancel(args.trans);
  350. goto out_unlock;
  351. }
  352. /*
  353. * Generic handler routine to remove a name from an attribute list.
  354. * Transitions attribute list from Btree to shortform as necessary.
  355. */
  356. int
  357. xfs_attr_remove(
  358. struct xfs_inode *dp,
  359. const unsigned char *name,
  360. int flags)
  361. {
  362. struct xfs_mount *mp = dp->i_mount;
  363. struct xfs_da_args args;
  364. int error;
  365. XFS_STATS_INC(mp, xs_attr_remove);
  366. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  367. return -EIO;
  368. error = xfs_attr_args_init(&args, dp, name, flags);
  369. if (error)
  370. return error;
  371. /*
  372. * we have no control over the attribute names that userspace passes us
  373. * to remove, so we have to allow the name lookup prior to attribute
  374. * removal to fail.
  375. */
  376. args.op_flags = XFS_DA_OP_OKNOENT;
  377. error = xfs_qm_dqattach(dp);
  378. if (error)
  379. return error;
  380. /*
  381. * Root fork attributes can use reserved data blocks for this
  382. * operation if necessary
  383. */
  384. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
  385. XFS_ATTRRM_SPACE_RES(mp), 0,
  386. (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
  387. &args.trans);
  388. if (error)
  389. return error;
  390. xfs_ilock(dp, XFS_ILOCK_EXCL);
  391. /*
  392. * No need to make quota reservations here. We expect to release some
  393. * blocks not allocate in the common case.
  394. */
  395. xfs_trans_ijoin(args.trans, dp, 0);
  396. error = xfs_attr_remove_args(&args);
  397. if (error)
  398. goto out;
  399. /*
  400. * If this is a synchronous mount, make sure that the
  401. * transaction goes to disk before returning to the user.
  402. */
  403. if (mp->m_flags & XFS_MOUNT_WSYNC)
  404. xfs_trans_set_sync(args.trans);
  405. if ((flags & ATTR_KERNOTIME) == 0)
  406. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  407. /*
  408. * Commit the last in the sequence of transactions.
  409. */
  410. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  411. error = xfs_trans_commit(args.trans);
  412. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  413. return error;
  414. out:
  415. if (args.trans)
  416. xfs_trans_cancel(args.trans);
  417. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  418. return error;
  419. }
  420. /*========================================================================
  421. * External routines when attribute list is inside the inode
  422. *========================================================================*/
  423. /*
  424. * Add a name to the shortform attribute list structure
  425. * This is the external routine.
  426. */
  427. STATIC int
  428. xfs_attr_shortform_addname(xfs_da_args_t *args)
  429. {
  430. int newsize, forkoff, retval;
  431. trace_xfs_attr_sf_addname(args);
  432. retval = xfs_attr_shortform_lookup(args);
  433. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  434. return retval;
  435. } else if (retval == -EEXIST) {
  436. if (args->flags & ATTR_CREATE)
  437. return retval;
  438. retval = xfs_attr_shortform_remove(args);
  439. if (retval)
  440. return retval;
  441. /*
  442. * Since we have removed the old attr, clear ATTR_REPLACE so
  443. * that the leaf format add routine won't trip over the attr
  444. * not being around.
  445. */
  446. args->flags &= ~ATTR_REPLACE;
  447. }
  448. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  449. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  450. return -ENOSPC;
  451. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  452. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  453. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  454. if (!forkoff)
  455. return -ENOSPC;
  456. xfs_attr_shortform_add(args, forkoff);
  457. return 0;
  458. }
  459. /*========================================================================
  460. * External routines when attribute list is one block
  461. *========================================================================*/
  462. /*
  463. * Add a name to the leaf attribute list structure
  464. *
  465. * This leaf block cannot have a "remote" value, we only call this routine
  466. * if bmap_one_block() says there is only one block (ie: no remote blks).
  467. */
  468. STATIC int
  469. xfs_attr_leaf_addname(
  470. struct xfs_da_args *args)
  471. {
  472. struct xfs_inode *dp;
  473. struct xfs_buf *bp;
  474. int retval, error, forkoff;
  475. trace_xfs_attr_leaf_addname(args);
  476. /*
  477. * Read the (only) block in the attribute list in.
  478. */
  479. dp = args->dp;
  480. args->blkno = 0;
  481. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  482. if (error)
  483. return error;
  484. /*
  485. * Look up the given attribute in the leaf block. Figure out if
  486. * the given flags produce an error or call for an atomic rename.
  487. */
  488. retval = xfs_attr3_leaf_lookup_int(bp, args);
  489. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  490. xfs_trans_brelse(args->trans, bp);
  491. return retval;
  492. } else if (retval == -EEXIST) {
  493. if (args->flags & ATTR_CREATE) { /* pure create op */
  494. xfs_trans_brelse(args->trans, bp);
  495. return retval;
  496. }
  497. trace_xfs_attr_leaf_replace(args);
  498. /* save the attribute state for later removal*/
  499. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  500. args->blkno2 = args->blkno; /* set 2nd entry info*/
  501. args->index2 = args->index;
  502. args->rmtblkno2 = args->rmtblkno;
  503. args->rmtblkcnt2 = args->rmtblkcnt;
  504. args->rmtvaluelen2 = args->rmtvaluelen;
  505. /*
  506. * clear the remote attr state now that it is saved so that the
  507. * values reflect the state of the attribute we are about to
  508. * add, not the attribute we just found and will remove later.
  509. */
  510. args->rmtblkno = 0;
  511. args->rmtblkcnt = 0;
  512. args->rmtvaluelen = 0;
  513. }
  514. /*
  515. * Add the attribute to the leaf block, transitioning to a Btree
  516. * if required.
  517. */
  518. retval = xfs_attr3_leaf_add(bp, args);
  519. if (retval == -ENOSPC) {
  520. /*
  521. * Promote the attribute list to the Btree format, then
  522. * Commit that transaction so that the node_addname() call
  523. * can manage its own transactions.
  524. */
  525. error = xfs_attr3_leaf_to_node(args);
  526. if (error)
  527. return error;
  528. error = xfs_defer_finish(&args->trans);
  529. if (error)
  530. return error;
  531. /*
  532. * Commit the current trans (including the inode) and start
  533. * a new one.
  534. */
  535. error = xfs_trans_roll_inode(&args->trans, dp);
  536. if (error)
  537. return error;
  538. /*
  539. * Fob the whole rest of the problem off on the Btree code.
  540. */
  541. error = xfs_attr_node_addname(args);
  542. return error;
  543. }
  544. /*
  545. * Commit the transaction that added the attr name so that
  546. * later routines can manage their own transactions.
  547. */
  548. error = xfs_trans_roll_inode(&args->trans, dp);
  549. if (error)
  550. return error;
  551. /*
  552. * If there was an out-of-line value, allocate the blocks we
  553. * identified for its storage and copy the value. This is done
  554. * after we create the attribute so that we don't overflow the
  555. * maximum size of a transaction and/or hit a deadlock.
  556. */
  557. if (args->rmtblkno > 0) {
  558. error = xfs_attr_rmtval_set(args);
  559. if (error)
  560. return error;
  561. }
  562. /*
  563. * If this is an atomic rename operation, we must "flip" the
  564. * incomplete flags on the "new" and "old" attribute/value pairs
  565. * so that one disappears and one appears atomically. Then we
  566. * must remove the "old" attribute/value pair.
  567. */
  568. if (args->op_flags & XFS_DA_OP_RENAME) {
  569. /*
  570. * In a separate transaction, set the incomplete flag on the
  571. * "old" attr and clear the incomplete flag on the "new" attr.
  572. */
  573. error = xfs_attr3_leaf_flipflags(args);
  574. if (error)
  575. return error;
  576. /*
  577. * Dismantle the "old" attribute/value pair by removing
  578. * a "remote" value (if it exists).
  579. */
  580. args->index = args->index2;
  581. args->blkno = args->blkno2;
  582. args->rmtblkno = args->rmtblkno2;
  583. args->rmtblkcnt = args->rmtblkcnt2;
  584. args->rmtvaluelen = args->rmtvaluelen2;
  585. if (args->rmtblkno) {
  586. error = xfs_attr_rmtval_remove(args);
  587. if (error)
  588. return error;
  589. }
  590. /*
  591. * Read in the block containing the "old" attr, then
  592. * remove the "old" attr from that block (neat, huh!)
  593. */
  594. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  595. -1, &bp);
  596. if (error)
  597. return error;
  598. xfs_attr3_leaf_remove(bp, args);
  599. /*
  600. * If the result is small enough, shrink it all into the inode.
  601. */
  602. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  603. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  604. /* bp is gone due to xfs_da_shrink_inode */
  605. if (error)
  606. return error;
  607. error = xfs_defer_finish(&args->trans);
  608. if (error)
  609. return error;
  610. }
  611. /*
  612. * Commit the remove and start the next trans in series.
  613. */
  614. error = xfs_trans_roll_inode(&args->trans, dp);
  615. } else if (args->rmtblkno > 0) {
  616. /*
  617. * Added a "remote" value, just clear the incomplete flag.
  618. */
  619. error = xfs_attr3_leaf_clearflag(args);
  620. }
  621. return error;
  622. }
  623. /*
  624. * Remove a name from the leaf attribute list structure
  625. *
  626. * This leaf block cannot have a "remote" value, we only call this routine
  627. * if bmap_one_block() says there is only one block (ie: no remote blks).
  628. */
  629. STATIC int
  630. xfs_attr_leaf_removename(
  631. struct xfs_da_args *args)
  632. {
  633. struct xfs_inode *dp;
  634. struct xfs_buf *bp;
  635. int error, forkoff;
  636. trace_xfs_attr_leaf_removename(args);
  637. /*
  638. * Remove the attribute.
  639. */
  640. dp = args->dp;
  641. args->blkno = 0;
  642. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  643. if (error)
  644. return error;
  645. error = xfs_attr3_leaf_lookup_int(bp, args);
  646. if (error == -ENOATTR) {
  647. xfs_trans_brelse(args->trans, bp);
  648. return error;
  649. }
  650. xfs_attr3_leaf_remove(bp, args);
  651. /*
  652. * If the result is small enough, shrink it all into the inode.
  653. */
  654. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  655. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  656. /* bp is gone due to xfs_da_shrink_inode */
  657. if (error)
  658. return error;
  659. error = xfs_defer_finish(&args->trans);
  660. if (error)
  661. return error;
  662. }
  663. return 0;
  664. }
  665. /*
  666. * Look up a name in a leaf attribute list structure.
  667. *
  668. * This leaf block cannot have a "remote" value, we only call this routine
  669. * if bmap_one_block() says there is only one block (ie: no remote blks).
  670. */
  671. STATIC int
  672. xfs_attr_leaf_get(xfs_da_args_t *args)
  673. {
  674. struct xfs_buf *bp;
  675. int error;
  676. trace_xfs_attr_leaf_get(args);
  677. args->blkno = 0;
  678. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  679. if (error)
  680. return error;
  681. error = xfs_attr3_leaf_lookup_int(bp, args);
  682. if (error != -EEXIST) {
  683. xfs_trans_brelse(args->trans, bp);
  684. return error;
  685. }
  686. error = xfs_attr3_leaf_getvalue(bp, args);
  687. xfs_trans_brelse(args->trans, bp);
  688. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  689. error = xfs_attr_rmtval_get(args);
  690. }
  691. return error;
  692. }
  693. /*========================================================================
  694. * External routines when attribute list size > geo->blksize
  695. *========================================================================*/
  696. /*
  697. * Add a name to a Btree-format attribute list.
  698. *
  699. * This will involve walking down the Btree, and may involve splitting
  700. * leaf nodes and even splitting intermediate nodes up to and including
  701. * the root node (a special case of an intermediate node).
  702. *
  703. * "Remote" attribute values confuse the issue and atomic rename operations
  704. * add a whole extra layer of confusion on top of that.
  705. */
  706. STATIC int
  707. xfs_attr_node_addname(
  708. struct xfs_da_args *args)
  709. {
  710. struct xfs_da_state *state;
  711. struct xfs_da_state_blk *blk;
  712. struct xfs_inode *dp;
  713. struct xfs_mount *mp;
  714. int retval, error;
  715. trace_xfs_attr_node_addname(args);
  716. /*
  717. * Fill in bucket of arguments/results/context to carry around.
  718. */
  719. dp = args->dp;
  720. mp = dp->i_mount;
  721. restart:
  722. state = xfs_da_state_alloc();
  723. state->args = args;
  724. state->mp = mp;
  725. /*
  726. * Search to see if name already exists, and get back a pointer
  727. * to where it should go.
  728. */
  729. error = xfs_da3_node_lookup_int(state, &retval);
  730. if (error)
  731. goto out;
  732. blk = &state->path.blk[ state->path.active-1 ];
  733. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  734. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  735. goto out;
  736. } else if (retval == -EEXIST) {
  737. if (args->flags & ATTR_CREATE)
  738. goto out;
  739. trace_xfs_attr_node_replace(args);
  740. /* save the attribute state for later removal*/
  741. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  742. args->blkno2 = args->blkno; /* set 2nd entry info*/
  743. args->index2 = args->index;
  744. args->rmtblkno2 = args->rmtblkno;
  745. args->rmtblkcnt2 = args->rmtblkcnt;
  746. args->rmtvaluelen2 = args->rmtvaluelen;
  747. /*
  748. * clear the remote attr state now that it is saved so that the
  749. * values reflect the state of the attribute we are about to
  750. * add, not the attribute we just found and will remove later.
  751. */
  752. args->rmtblkno = 0;
  753. args->rmtblkcnt = 0;
  754. args->rmtvaluelen = 0;
  755. }
  756. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  757. if (retval == -ENOSPC) {
  758. if (state->path.active == 1) {
  759. /*
  760. * Its really a single leaf node, but it had
  761. * out-of-line values so it looked like it *might*
  762. * have been a b-tree.
  763. */
  764. xfs_da_state_free(state);
  765. state = NULL;
  766. error = xfs_attr3_leaf_to_node(args);
  767. if (error)
  768. goto out;
  769. error = xfs_defer_finish(&args->trans);
  770. if (error)
  771. goto out;
  772. /*
  773. * Commit the node conversion and start the next
  774. * trans in the chain.
  775. */
  776. error = xfs_trans_roll_inode(&args->trans, dp);
  777. if (error)
  778. goto out;
  779. goto restart;
  780. }
  781. /*
  782. * Split as many Btree elements as required.
  783. * This code tracks the new and old attr's location
  784. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  785. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  786. */
  787. error = xfs_da3_split(state);
  788. if (error)
  789. goto out;
  790. error = xfs_defer_finish(&args->trans);
  791. if (error)
  792. goto out;
  793. } else {
  794. /*
  795. * Addition succeeded, update Btree hashvals.
  796. */
  797. xfs_da3_fixhashpath(state, &state->path);
  798. }
  799. /*
  800. * Kill the state structure, we're done with it and need to
  801. * allow the buffers to come back later.
  802. */
  803. xfs_da_state_free(state);
  804. state = NULL;
  805. /*
  806. * Commit the leaf addition or btree split and start the next
  807. * trans in the chain.
  808. */
  809. error = xfs_trans_roll_inode(&args->trans, dp);
  810. if (error)
  811. goto out;
  812. /*
  813. * If there was an out-of-line value, allocate the blocks we
  814. * identified for its storage and copy the value. This is done
  815. * after we create the attribute so that we don't overflow the
  816. * maximum size of a transaction and/or hit a deadlock.
  817. */
  818. if (args->rmtblkno > 0) {
  819. error = xfs_attr_rmtval_set(args);
  820. if (error)
  821. return error;
  822. }
  823. /*
  824. * If this is an atomic rename operation, we must "flip" the
  825. * incomplete flags on the "new" and "old" attribute/value pairs
  826. * so that one disappears and one appears atomically. Then we
  827. * must remove the "old" attribute/value pair.
  828. */
  829. if (args->op_flags & XFS_DA_OP_RENAME) {
  830. /*
  831. * In a separate transaction, set the incomplete flag on the
  832. * "old" attr and clear the incomplete flag on the "new" attr.
  833. */
  834. error = xfs_attr3_leaf_flipflags(args);
  835. if (error)
  836. goto out;
  837. /*
  838. * Dismantle the "old" attribute/value pair by removing
  839. * a "remote" value (if it exists).
  840. */
  841. args->index = args->index2;
  842. args->blkno = args->blkno2;
  843. args->rmtblkno = args->rmtblkno2;
  844. args->rmtblkcnt = args->rmtblkcnt2;
  845. args->rmtvaluelen = args->rmtvaluelen2;
  846. if (args->rmtblkno) {
  847. error = xfs_attr_rmtval_remove(args);
  848. if (error)
  849. return error;
  850. }
  851. /*
  852. * Re-find the "old" attribute entry after any split ops.
  853. * The INCOMPLETE flag means that we will find the "old"
  854. * attr, not the "new" one.
  855. */
  856. args->flags |= XFS_ATTR_INCOMPLETE;
  857. state = xfs_da_state_alloc();
  858. state->args = args;
  859. state->mp = mp;
  860. state->inleaf = 0;
  861. error = xfs_da3_node_lookup_int(state, &retval);
  862. if (error)
  863. goto out;
  864. /*
  865. * Remove the name and update the hashvals in the tree.
  866. */
  867. blk = &state->path.blk[ state->path.active-1 ];
  868. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  869. error = xfs_attr3_leaf_remove(blk->bp, args);
  870. xfs_da3_fixhashpath(state, &state->path);
  871. /*
  872. * Check to see if the tree needs to be collapsed.
  873. */
  874. if (retval && (state->path.active > 1)) {
  875. error = xfs_da3_join(state);
  876. if (error)
  877. goto out;
  878. error = xfs_defer_finish(&args->trans);
  879. if (error)
  880. goto out;
  881. }
  882. /*
  883. * Commit and start the next trans in the chain.
  884. */
  885. error = xfs_trans_roll_inode(&args->trans, dp);
  886. if (error)
  887. goto out;
  888. } else if (args->rmtblkno > 0) {
  889. /*
  890. * Added a "remote" value, just clear the incomplete flag.
  891. */
  892. error = xfs_attr3_leaf_clearflag(args);
  893. if (error)
  894. goto out;
  895. }
  896. retval = error = 0;
  897. out:
  898. if (state)
  899. xfs_da_state_free(state);
  900. if (error)
  901. return error;
  902. return retval;
  903. }
  904. /*
  905. * Remove a name from a B-tree attribute list.
  906. *
  907. * This will involve walking down the Btree, and may involve joining
  908. * leaf nodes and even joining intermediate nodes up to and including
  909. * the root node (a special case of an intermediate node).
  910. */
  911. STATIC int
  912. xfs_attr_node_removename(
  913. struct xfs_da_args *args)
  914. {
  915. struct xfs_da_state *state;
  916. struct xfs_da_state_blk *blk;
  917. struct xfs_inode *dp;
  918. struct xfs_buf *bp;
  919. int retval, error, forkoff;
  920. trace_xfs_attr_node_removename(args);
  921. /*
  922. * Tie a string around our finger to remind us where we are.
  923. */
  924. dp = args->dp;
  925. state = xfs_da_state_alloc();
  926. state->args = args;
  927. state->mp = dp->i_mount;
  928. /*
  929. * Search to see if name exists, and get back a pointer to it.
  930. */
  931. error = xfs_da3_node_lookup_int(state, &retval);
  932. if (error || (retval != -EEXIST)) {
  933. if (error == 0)
  934. error = retval;
  935. goto out;
  936. }
  937. /*
  938. * If there is an out-of-line value, de-allocate the blocks.
  939. * This is done before we remove the attribute so that we don't
  940. * overflow the maximum size of a transaction and/or hit a deadlock.
  941. */
  942. blk = &state->path.blk[ state->path.active-1 ];
  943. ASSERT(blk->bp != NULL);
  944. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  945. if (args->rmtblkno > 0) {
  946. /*
  947. * Fill in disk block numbers in the state structure
  948. * so that we can get the buffers back after we commit
  949. * several transactions in the following calls.
  950. */
  951. error = xfs_attr_fillstate(state);
  952. if (error)
  953. goto out;
  954. /*
  955. * Mark the attribute as INCOMPLETE, then bunmapi() the
  956. * remote value.
  957. */
  958. error = xfs_attr3_leaf_setflag(args);
  959. if (error)
  960. goto out;
  961. error = xfs_attr_rmtval_remove(args);
  962. if (error)
  963. goto out;
  964. /*
  965. * Refill the state structure with buffers, the prior calls
  966. * released our buffers.
  967. */
  968. error = xfs_attr_refillstate(state);
  969. if (error)
  970. goto out;
  971. }
  972. /*
  973. * Remove the name and update the hashvals in the tree.
  974. */
  975. blk = &state->path.blk[ state->path.active-1 ];
  976. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  977. retval = xfs_attr3_leaf_remove(blk->bp, args);
  978. xfs_da3_fixhashpath(state, &state->path);
  979. /*
  980. * Check to see if the tree needs to be collapsed.
  981. */
  982. if (retval && (state->path.active > 1)) {
  983. error = xfs_da3_join(state);
  984. if (error)
  985. goto out;
  986. error = xfs_defer_finish(&args->trans);
  987. if (error)
  988. goto out;
  989. /*
  990. * Commit the Btree join operation and start a new trans.
  991. */
  992. error = xfs_trans_roll_inode(&args->trans, dp);
  993. if (error)
  994. goto out;
  995. }
  996. /*
  997. * If the result is small enough, push it all into the inode.
  998. */
  999. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1000. /*
  1001. * Have to get rid of the copy of this dabuf in the state.
  1002. */
  1003. ASSERT(state->path.active == 1);
  1004. ASSERT(state->path.blk[0].bp);
  1005. state->path.blk[0].bp = NULL;
  1006. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
  1007. if (error)
  1008. goto out;
  1009. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1010. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1011. /* bp is gone due to xfs_da_shrink_inode */
  1012. if (error)
  1013. goto out;
  1014. error = xfs_defer_finish(&args->trans);
  1015. if (error)
  1016. goto out;
  1017. } else
  1018. xfs_trans_brelse(args->trans, bp);
  1019. }
  1020. error = 0;
  1021. out:
  1022. xfs_da_state_free(state);
  1023. return error;
  1024. }
  1025. /*
  1026. * Fill in the disk block numbers in the state structure for the buffers
  1027. * that are attached to the state structure.
  1028. * This is done so that we can quickly reattach ourselves to those buffers
  1029. * after some set of transaction commits have released these buffers.
  1030. */
  1031. STATIC int
  1032. xfs_attr_fillstate(xfs_da_state_t *state)
  1033. {
  1034. xfs_da_state_path_t *path;
  1035. xfs_da_state_blk_t *blk;
  1036. int level;
  1037. trace_xfs_attr_fillstate(state->args);
  1038. /*
  1039. * Roll down the "path" in the state structure, storing the on-disk
  1040. * block number for those buffers in the "path".
  1041. */
  1042. path = &state->path;
  1043. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1044. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1045. if (blk->bp) {
  1046. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1047. blk->bp = NULL;
  1048. } else {
  1049. blk->disk_blkno = 0;
  1050. }
  1051. }
  1052. /*
  1053. * Roll down the "altpath" in the state structure, storing the on-disk
  1054. * block number for those buffers in the "altpath".
  1055. */
  1056. path = &state->altpath;
  1057. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1058. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1059. if (blk->bp) {
  1060. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1061. blk->bp = NULL;
  1062. } else {
  1063. blk->disk_blkno = 0;
  1064. }
  1065. }
  1066. return 0;
  1067. }
  1068. /*
  1069. * Reattach the buffers to the state structure based on the disk block
  1070. * numbers stored in the state structure.
  1071. * This is done after some set of transaction commits have released those
  1072. * buffers from our grip.
  1073. */
  1074. STATIC int
  1075. xfs_attr_refillstate(xfs_da_state_t *state)
  1076. {
  1077. xfs_da_state_path_t *path;
  1078. xfs_da_state_blk_t *blk;
  1079. int level, error;
  1080. trace_xfs_attr_refillstate(state->args);
  1081. /*
  1082. * Roll down the "path" in the state structure, storing the on-disk
  1083. * block number for those buffers in the "path".
  1084. */
  1085. path = &state->path;
  1086. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1087. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1088. if (blk->disk_blkno) {
  1089. error = xfs_da3_node_read(state->args->trans,
  1090. state->args->dp,
  1091. blk->blkno, blk->disk_blkno,
  1092. &blk->bp, XFS_ATTR_FORK);
  1093. if (error)
  1094. return error;
  1095. } else {
  1096. blk->bp = NULL;
  1097. }
  1098. }
  1099. /*
  1100. * Roll down the "altpath" in the state structure, storing the on-disk
  1101. * block number for those buffers in the "altpath".
  1102. */
  1103. path = &state->altpath;
  1104. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1105. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1106. if (blk->disk_blkno) {
  1107. error = xfs_da3_node_read(state->args->trans,
  1108. state->args->dp,
  1109. blk->blkno, blk->disk_blkno,
  1110. &blk->bp, XFS_ATTR_FORK);
  1111. if (error)
  1112. return error;
  1113. } else {
  1114. blk->bp = NULL;
  1115. }
  1116. }
  1117. return 0;
  1118. }
  1119. /*
  1120. * Look up a filename in a node attribute list.
  1121. *
  1122. * This routine gets called for any attribute fork that has more than one
  1123. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1124. * "remote" values taking up more blocks.
  1125. */
  1126. STATIC int
  1127. xfs_attr_node_get(xfs_da_args_t *args)
  1128. {
  1129. xfs_da_state_t *state;
  1130. xfs_da_state_blk_t *blk;
  1131. int error, retval;
  1132. int i;
  1133. trace_xfs_attr_node_get(args);
  1134. state = xfs_da_state_alloc();
  1135. state->args = args;
  1136. state->mp = args->dp->i_mount;
  1137. /*
  1138. * Search to see if name exists, and get back a pointer to it.
  1139. */
  1140. error = xfs_da3_node_lookup_int(state, &retval);
  1141. if (error) {
  1142. retval = error;
  1143. } else if (retval == -EEXIST) {
  1144. blk = &state->path.blk[ state->path.active-1 ];
  1145. ASSERT(blk->bp != NULL);
  1146. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1147. /*
  1148. * Get the value, local or "remote"
  1149. */
  1150. retval = xfs_attr3_leaf_getvalue(blk->bp, args);
  1151. if (!retval && (args->rmtblkno > 0)
  1152. && !(args->flags & ATTR_KERNOVAL)) {
  1153. retval = xfs_attr_rmtval_get(args);
  1154. }
  1155. }
  1156. /*
  1157. * If not in a transaction, we have to release all the buffers.
  1158. */
  1159. for (i = 0; i < state->path.active; i++) {
  1160. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1161. state->path.blk[i].bp = NULL;
  1162. }
  1163. xfs_da_state_free(state);
  1164. return retval;
  1165. }