xfs_attr.c 33 KB

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