xfs_attr.c 33 KB

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