xfs_attr.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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_format.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_trans_priv.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_attr_sf.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_bmap.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_vnodeops.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. STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
  63. /*
  64. * Internal routines when attribute list is more than one block.
  65. */
  66. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  67. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  68. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  69. STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
  70. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  71. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  72. STATIC int
  73. xfs_attr_name_to_xname(
  74. struct xfs_name *xname,
  75. const unsigned char *aname)
  76. {
  77. if (!aname)
  78. return EINVAL;
  79. xname->name = aname;
  80. xname->len = strlen((char *)aname);
  81. if (xname->len >= MAXNAMELEN)
  82. return EFAULT; /* match IRIX behaviour */
  83. return 0;
  84. }
  85. STATIC int
  86. xfs_inode_hasattr(
  87. struct xfs_inode *ip)
  88. {
  89. if (!XFS_IFORK_Q(ip) ||
  90. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  91. ip->i_d.di_anextents == 0))
  92. return 0;
  93. return 1;
  94. }
  95. /*========================================================================
  96. * Overall external interface routines.
  97. *========================================================================*/
  98. STATIC int
  99. xfs_attr_get_int(
  100. struct xfs_inode *ip,
  101. struct xfs_name *name,
  102. unsigned char *value,
  103. int *valuelenp,
  104. int flags)
  105. {
  106. xfs_da_args_t args;
  107. int error;
  108. if (!xfs_inode_hasattr(ip))
  109. return ENOATTR;
  110. /*
  111. * Fill in the arg structure for this request.
  112. */
  113. memset((char *)&args, 0, sizeof(args));
  114. args.name = name->name;
  115. args.namelen = name->len;
  116. args.value = value;
  117. args.valuelen = *valuelenp;
  118. args.flags = flags;
  119. args.hashval = xfs_da_hashname(args.name, args.namelen);
  120. args.dp = ip;
  121. args.whichfork = XFS_ATTR_FORK;
  122. /*
  123. * Decide on what work routines to call based on the inode size.
  124. */
  125. if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  126. error = xfs_attr_shortform_getvalue(&args);
  127. } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
  128. error = xfs_attr_leaf_get(&args);
  129. } else {
  130. error = xfs_attr_node_get(&args);
  131. }
  132. /*
  133. * Return the number of bytes in the value to the caller.
  134. */
  135. *valuelenp = args.valuelen;
  136. if (error == EEXIST)
  137. error = 0;
  138. return(error);
  139. }
  140. int
  141. xfs_attr_get(
  142. xfs_inode_t *ip,
  143. const unsigned char *name,
  144. unsigned char *value,
  145. int *valuelenp,
  146. int flags)
  147. {
  148. int error;
  149. struct xfs_name xname;
  150. XFS_STATS_INC(xs_attr_get);
  151. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  152. return(EIO);
  153. error = xfs_attr_name_to_xname(&xname, name);
  154. if (error)
  155. return error;
  156. xfs_ilock(ip, XFS_ILOCK_SHARED);
  157. error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
  158. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  159. return(error);
  160. }
  161. /*
  162. * Calculate how many blocks we need for the new attribute,
  163. */
  164. STATIC int
  165. xfs_attr_calc_size(
  166. struct xfs_inode *ip,
  167. int namelen,
  168. int valuelen,
  169. int *local)
  170. {
  171. struct xfs_mount *mp = ip->i_mount;
  172. int size;
  173. int nblks;
  174. /*
  175. * Determine space new attribute will use, and if it would be
  176. * "local" or "remote" (note: local != inline).
  177. */
  178. size = xfs_attr_leaf_newentsize(namelen, valuelen,
  179. mp->m_sb.sb_blocksize, local);
  180. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  181. if (*local) {
  182. if (size > (mp->m_sb.sb_blocksize >> 1)) {
  183. /* Double split possible */
  184. nblks *= 2;
  185. }
  186. } else {
  187. /*
  188. * Out of line attribute, cannot double split, but
  189. * make room for the attribute value itself.
  190. */
  191. uint dblocks = XFS_B_TO_FSB(mp, valuelen);
  192. nblks += dblocks;
  193. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  194. }
  195. return nblks;
  196. }
  197. STATIC int
  198. xfs_attr_set_int(
  199. struct xfs_inode *dp,
  200. struct xfs_name *name,
  201. unsigned char *value,
  202. int valuelen,
  203. int flags)
  204. {
  205. xfs_da_args_t args;
  206. xfs_fsblock_t firstblock;
  207. xfs_bmap_free_t flist;
  208. int error, err2, committed;
  209. xfs_mount_t *mp = dp->i_mount;
  210. int rsvd = (flags & ATTR_ROOT) != 0;
  211. int local;
  212. /*
  213. * Attach the dquots to the inode.
  214. */
  215. error = xfs_qm_dqattach(dp, 0);
  216. if (error)
  217. return error;
  218. /*
  219. * If the inode doesn't have an attribute fork, add one.
  220. * (inode must not be locked when we call this routine)
  221. */
  222. if (XFS_IFORK_Q(dp) == 0) {
  223. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  224. XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
  225. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  226. return(error);
  227. }
  228. /*
  229. * Fill in the arg structure for this request.
  230. */
  231. memset((char *)&args, 0, sizeof(args));
  232. args.name = name->name;
  233. args.namelen = name->len;
  234. args.value = value;
  235. args.valuelen = valuelen;
  236. args.flags = flags;
  237. args.hashval = xfs_da_hashname(args.name, args.namelen);
  238. args.dp = dp;
  239. args.firstblock = &firstblock;
  240. args.flist = &flist;
  241. args.whichfork = XFS_ATTR_FORK;
  242. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  243. /* Size is now blocks for attribute data */
  244. args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
  245. /*
  246. * Start our first transaction of the day.
  247. *
  248. * All future transactions during this code must be "chained" off
  249. * this one via the trans_dup() call. All transactions will contain
  250. * the inode, and the inode will always be marked with trans_ihold().
  251. * Since the inode will be locked in all transactions, we must log
  252. * the inode in every transaction to let it float upward through
  253. * the log.
  254. */
  255. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  256. /*
  257. * Root fork attributes can use reserved data blocks for this
  258. * operation if necessary
  259. */
  260. if (rsvd)
  261. args.trans->t_flags |= XFS_TRANS_RESERVE;
  262. error = xfs_trans_reserve(args.trans, args.total,
  263. XFS_ATTRSETM_LOG_RES(mp) +
  264. XFS_ATTRSETRT_LOG_RES(mp) * args.total,
  265. 0, XFS_TRANS_PERM_LOG_RES,
  266. XFS_ATTRSET_LOG_COUNT);
  267. if (error) {
  268. xfs_trans_cancel(args.trans, 0);
  269. return(error);
  270. }
  271. xfs_ilock(dp, XFS_ILOCK_EXCL);
  272. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  273. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  274. XFS_QMOPT_RES_REGBLKS);
  275. if (error) {
  276. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  277. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  278. return (error);
  279. }
  280. xfs_trans_ijoin(args.trans, dp, 0);
  281. /*
  282. * If the attribute list is non-existent or a shortform list,
  283. * upgrade it to a single-leaf-block attribute list.
  284. */
  285. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  286. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  287. (dp->i_d.di_anextents == 0))) {
  288. /*
  289. * Build initial attribute list (if required).
  290. */
  291. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  292. xfs_attr_shortform_create(&args);
  293. /*
  294. * Try to add the attr to the attribute list in
  295. * the inode.
  296. */
  297. error = xfs_attr_shortform_addname(&args);
  298. if (error != ENOSPC) {
  299. /*
  300. * Commit the shortform mods, and we're done.
  301. * NOTE: this is also the error path (EEXIST, etc).
  302. */
  303. ASSERT(args.trans != NULL);
  304. /*
  305. * If this is a synchronous mount, make sure that
  306. * the transaction goes to disk before returning
  307. * to the user.
  308. */
  309. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  310. xfs_trans_set_sync(args.trans);
  311. }
  312. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  313. xfs_trans_ichgtime(args.trans, dp,
  314. XFS_ICHGTIME_CHG);
  315. }
  316. err2 = xfs_trans_commit(args.trans,
  317. XFS_TRANS_RELEASE_LOG_RES);
  318. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  319. return(error == 0 ? err2 : error);
  320. }
  321. /*
  322. * It won't fit in the shortform, transform to a leaf block.
  323. * GROT: another possible req'mt for a double-split btree op.
  324. */
  325. xfs_bmap_init(args.flist, args.firstblock);
  326. error = xfs_attr_shortform_to_leaf(&args);
  327. if (!error) {
  328. error = xfs_bmap_finish(&args.trans, args.flist,
  329. &committed);
  330. }
  331. if (error) {
  332. ASSERT(committed);
  333. args.trans = NULL;
  334. xfs_bmap_cancel(&flist);
  335. goto out;
  336. }
  337. /*
  338. * bmap_finish() may have committed the last trans and started
  339. * a new one. We need the inode to be in all transactions.
  340. */
  341. if (committed)
  342. xfs_trans_ijoin(args.trans, dp, 0);
  343. /*
  344. * Commit the leaf transformation. We'll need another (linked)
  345. * transaction to add the new attribute to the leaf.
  346. */
  347. error = xfs_trans_roll(&args.trans, dp);
  348. if (error)
  349. goto out;
  350. }
  351. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  352. error = xfs_attr_leaf_addname(&args);
  353. } else {
  354. error = xfs_attr_node_addname(&args);
  355. }
  356. if (error) {
  357. goto out;
  358. }
  359. /*
  360. * If this is a synchronous mount, make sure that the
  361. * transaction goes to disk before returning to the user.
  362. */
  363. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  364. xfs_trans_set_sync(args.trans);
  365. }
  366. if ((flags & ATTR_KERNOTIME) == 0)
  367. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  368. /*
  369. * Commit the last in the sequence of transactions.
  370. */
  371. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  372. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  373. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  374. return(error);
  375. out:
  376. if (args.trans)
  377. xfs_trans_cancel(args.trans,
  378. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  379. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  380. return(error);
  381. }
  382. int
  383. xfs_attr_set(
  384. xfs_inode_t *dp,
  385. const unsigned char *name,
  386. unsigned char *value,
  387. int valuelen,
  388. int flags)
  389. {
  390. int error;
  391. struct xfs_name xname;
  392. XFS_STATS_INC(xs_attr_set);
  393. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  394. return (EIO);
  395. error = xfs_attr_name_to_xname(&xname, name);
  396. if (error)
  397. return error;
  398. return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
  399. }
  400. /*
  401. * Generic handler routine to remove a name from an attribute list.
  402. * Transitions attribute list from Btree to shortform as necessary.
  403. */
  404. STATIC int
  405. xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
  406. {
  407. xfs_da_args_t args;
  408. xfs_fsblock_t firstblock;
  409. xfs_bmap_free_t flist;
  410. int error;
  411. xfs_mount_t *mp = dp->i_mount;
  412. /*
  413. * Fill in the arg structure for this request.
  414. */
  415. memset((char *)&args, 0, sizeof(args));
  416. args.name = name->name;
  417. args.namelen = name->len;
  418. args.flags = flags;
  419. args.hashval = xfs_da_hashname(args.name, args.namelen);
  420. args.dp = dp;
  421. args.firstblock = &firstblock;
  422. args.flist = &flist;
  423. args.total = 0;
  424. args.whichfork = XFS_ATTR_FORK;
  425. /*
  426. * we have no control over the attribute names that userspace passes us
  427. * to remove, so we have to allow the name lookup prior to attribute
  428. * removal to fail.
  429. */
  430. args.op_flags = XFS_DA_OP_OKNOENT;
  431. /*
  432. * Attach the dquots to the inode.
  433. */
  434. error = xfs_qm_dqattach(dp, 0);
  435. if (error)
  436. return error;
  437. /*
  438. * Start our first transaction of the day.
  439. *
  440. * All future transactions during this code must be "chained" off
  441. * this one via the trans_dup() call. All transactions will contain
  442. * the inode, and the inode will always be marked with trans_ihold().
  443. * Since the inode will be locked in all transactions, we must log
  444. * the inode in every transaction to let it float upward through
  445. * the log.
  446. */
  447. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  448. /*
  449. * Root fork attributes can use reserved data blocks for this
  450. * operation if necessary
  451. */
  452. if (flags & ATTR_ROOT)
  453. args.trans->t_flags |= XFS_TRANS_RESERVE;
  454. if ((error = xfs_trans_reserve(args.trans,
  455. XFS_ATTRRM_SPACE_RES(mp),
  456. XFS_ATTRRM_LOG_RES(mp),
  457. 0, XFS_TRANS_PERM_LOG_RES,
  458. XFS_ATTRRM_LOG_COUNT))) {
  459. xfs_trans_cancel(args.trans, 0);
  460. return(error);
  461. }
  462. xfs_ilock(dp, XFS_ILOCK_EXCL);
  463. /*
  464. * No need to make quota reservations here. We expect to release some
  465. * blocks not allocate in the common case.
  466. */
  467. xfs_trans_ijoin(args.trans, dp, 0);
  468. /*
  469. * Decide on what work routines to call based on the inode size.
  470. */
  471. if (!xfs_inode_hasattr(dp)) {
  472. error = XFS_ERROR(ENOATTR);
  473. goto out;
  474. }
  475. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  476. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  477. error = xfs_attr_shortform_remove(&args);
  478. if (error) {
  479. goto out;
  480. }
  481. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  482. error = xfs_attr_leaf_removename(&args);
  483. } else {
  484. error = xfs_attr_node_removename(&args);
  485. }
  486. if (error) {
  487. goto out;
  488. }
  489. /*
  490. * If this is a synchronous mount, make sure that the
  491. * transaction goes to disk before returning to the user.
  492. */
  493. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  494. xfs_trans_set_sync(args.trans);
  495. }
  496. if ((flags & ATTR_KERNOTIME) == 0)
  497. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  498. /*
  499. * Commit the last in the sequence of transactions.
  500. */
  501. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  502. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  503. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  504. return(error);
  505. out:
  506. if (args.trans)
  507. xfs_trans_cancel(args.trans,
  508. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  509. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  510. return(error);
  511. }
  512. int
  513. xfs_attr_remove(
  514. xfs_inode_t *dp,
  515. const unsigned char *name,
  516. int flags)
  517. {
  518. int error;
  519. struct xfs_name xname;
  520. XFS_STATS_INC(xs_attr_remove);
  521. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  522. return (EIO);
  523. error = xfs_attr_name_to_xname(&xname, name);
  524. if (error)
  525. return error;
  526. xfs_ilock(dp, XFS_ILOCK_SHARED);
  527. if (!xfs_inode_hasattr(dp)) {
  528. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  529. return XFS_ERROR(ENOATTR);
  530. }
  531. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  532. return xfs_attr_remove_int(dp, &xname, flags);
  533. }
  534. int
  535. xfs_attr_list_int(xfs_attr_list_context_t *context)
  536. {
  537. int error;
  538. xfs_inode_t *dp = context->dp;
  539. XFS_STATS_INC(xs_attr_list);
  540. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  541. return EIO;
  542. xfs_ilock(dp, XFS_ILOCK_SHARED);
  543. /*
  544. * Decide on what work routines to call based on the inode size.
  545. */
  546. if (!xfs_inode_hasattr(dp)) {
  547. error = 0;
  548. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  549. error = xfs_attr_shortform_list(context);
  550. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  551. error = xfs_attr_leaf_list(context);
  552. } else {
  553. error = xfs_attr_node_list(context);
  554. }
  555. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  556. return error;
  557. }
  558. #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
  559. (((struct attrlist_ent *) 0)->a_name - (char *) 0)
  560. #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
  561. ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
  562. & ~(sizeof(u_int32_t)-1))
  563. /*
  564. * Format an attribute and copy it out to the user's buffer.
  565. * Take care to check values and protect against them changing later,
  566. * we may be reading them directly out of a user buffer.
  567. */
  568. /*ARGSUSED*/
  569. STATIC int
  570. xfs_attr_put_listent(
  571. xfs_attr_list_context_t *context,
  572. int flags,
  573. unsigned char *name,
  574. int namelen,
  575. int valuelen,
  576. unsigned char *value)
  577. {
  578. struct attrlist *alist = (struct attrlist *)context->alist;
  579. attrlist_ent_t *aep;
  580. int arraytop;
  581. ASSERT(!(context->flags & ATTR_KERNOVAL));
  582. ASSERT(context->count >= 0);
  583. ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
  584. ASSERT(context->firstu >= sizeof(*alist));
  585. ASSERT(context->firstu <= context->bufsize);
  586. /*
  587. * Only list entries in the right namespace.
  588. */
  589. if (((context->flags & ATTR_SECURE) == 0) !=
  590. ((flags & XFS_ATTR_SECURE) == 0))
  591. return 0;
  592. if (((context->flags & ATTR_ROOT) == 0) !=
  593. ((flags & XFS_ATTR_ROOT) == 0))
  594. return 0;
  595. arraytop = sizeof(*alist) +
  596. context->count * sizeof(alist->al_offset[0]);
  597. context->firstu -= ATTR_ENTSIZE(namelen);
  598. if (context->firstu < arraytop) {
  599. trace_xfs_attr_list_full(context);
  600. alist->al_more = 1;
  601. context->seen_enough = 1;
  602. return 1;
  603. }
  604. aep = (attrlist_ent_t *)&context->alist[context->firstu];
  605. aep->a_valuelen = valuelen;
  606. memcpy(aep->a_name, name, namelen);
  607. aep->a_name[namelen] = 0;
  608. alist->al_offset[context->count++] = context->firstu;
  609. alist->al_count = context->count;
  610. trace_xfs_attr_list_add(context);
  611. return 0;
  612. }
  613. /*
  614. * Generate a list of extended attribute names and optionally
  615. * also value lengths. Positive return value follows the XFS
  616. * convention of being an error, zero or negative return code
  617. * is the length of the buffer returned (negated), indicating
  618. * success.
  619. */
  620. int
  621. xfs_attr_list(
  622. xfs_inode_t *dp,
  623. char *buffer,
  624. int bufsize,
  625. int flags,
  626. attrlist_cursor_kern_t *cursor)
  627. {
  628. xfs_attr_list_context_t context;
  629. struct attrlist *alist;
  630. int error;
  631. /*
  632. * Validate the cursor.
  633. */
  634. if (cursor->pad1 || cursor->pad2)
  635. return(XFS_ERROR(EINVAL));
  636. if ((cursor->initted == 0) &&
  637. (cursor->hashval || cursor->blkno || cursor->offset))
  638. return XFS_ERROR(EINVAL);
  639. /*
  640. * Check for a properly aligned buffer.
  641. */
  642. if (((long)buffer) & (sizeof(int)-1))
  643. return XFS_ERROR(EFAULT);
  644. if (flags & ATTR_KERNOVAL)
  645. bufsize = 0;
  646. /*
  647. * Initialize the output buffer.
  648. */
  649. memset(&context, 0, sizeof(context));
  650. context.dp = dp;
  651. context.cursor = cursor;
  652. context.resynch = 1;
  653. context.flags = flags;
  654. context.alist = buffer;
  655. context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
  656. context.firstu = context.bufsize;
  657. context.put_listent = xfs_attr_put_listent;
  658. alist = (struct attrlist *)context.alist;
  659. alist->al_count = 0;
  660. alist->al_more = 0;
  661. alist->al_offset[0] = context.bufsize;
  662. error = xfs_attr_list_int(&context);
  663. ASSERT(error >= 0);
  664. return error;
  665. }
  666. int /* error */
  667. xfs_attr_inactive(xfs_inode_t *dp)
  668. {
  669. xfs_trans_t *trans;
  670. xfs_mount_t *mp;
  671. int error;
  672. mp = dp->i_mount;
  673. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  674. xfs_ilock(dp, XFS_ILOCK_SHARED);
  675. if (!xfs_inode_hasattr(dp) ||
  676. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  677. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  678. return 0;
  679. }
  680. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  681. /*
  682. * Start our first transaction of the day.
  683. *
  684. * All future transactions during this code must be "chained" off
  685. * this one via the trans_dup() call. All transactions will contain
  686. * the inode, and the inode will always be marked with trans_ihold().
  687. * Since the inode will be locked in all transactions, we must log
  688. * the inode in every transaction to let it float upward through
  689. * the log.
  690. */
  691. trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
  692. if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
  693. XFS_TRANS_PERM_LOG_RES,
  694. XFS_ATTRINVAL_LOG_COUNT))) {
  695. xfs_trans_cancel(trans, 0);
  696. return(error);
  697. }
  698. xfs_ilock(dp, XFS_ILOCK_EXCL);
  699. /*
  700. * No need to make quota reservations here. We expect to release some
  701. * blocks, not allocate, in the common case.
  702. */
  703. xfs_trans_ijoin(trans, dp, 0);
  704. /*
  705. * Decide on what work routines to call based on the inode size.
  706. */
  707. if (!xfs_inode_hasattr(dp) ||
  708. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  709. error = 0;
  710. goto out;
  711. }
  712. error = xfs_attr3_root_inactive(&trans, dp);
  713. if (error)
  714. goto out;
  715. error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
  716. if (error)
  717. goto out;
  718. error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
  719. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  720. return(error);
  721. out:
  722. xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  723. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  724. return(error);
  725. }
  726. /*========================================================================
  727. * External routines when attribute list is inside the inode
  728. *========================================================================*/
  729. /*
  730. * Add a name to the shortform attribute list structure
  731. * This is the external routine.
  732. */
  733. STATIC int
  734. xfs_attr_shortform_addname(xfs_da_args_t *args)
  735. {
  736. int newsize, forkoff, retval;
  737. trace_xfs_attr_sf_addname(args);
  738. retval = xfs_attr_shortform_lookup(args);
  739. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  740. return(retval);
  741. } else if (retval == EEXIST) {
  742. if (args->flags & ATTR_CREATE)
  743. return(retval);
  744. retval = xfs_attr_shortform_remove(args);
  745. ASSERT(retval == 0);
  746. }
  747. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  748. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  749. return(XFS_ERROR(ENOSPC));
  750. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  751. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  752. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  753. if (!forkoff)
  754. return(XFS_ERROR(ENOSPC));
  755. xfs_attr_shortform_add(args, forkoff);
  756. return(0);
  757. }
  758. /*========================================================================
  759. * External routines when attribute list is one block
  760. *========================================================================*/
  761. /*
  762. * Add a name to the leaf attribute list structure
  763. *
  764. * This leaf block cannot have a "remote" value, we only call this routine
  765. * if bmap_one_block() says there is only one block (ie: no remote blks).
  766. */
  767. STATIC int
  768. xfs_attr_leaf_addname(xfs_da_args_t *args)
  769. {
  770. xfs_inode_t *dp;
  771. struct xfs_buf *bp;
  772. int retval, error, committed, forkoff;
  773. trace_xfs_attr_leaf_addname(args);
  774. /*
  775. * Read the (only) block in the attribute list in.
  776. */
  777. dp = args->dp;
  778. args->blkno = 0;
  779. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  780. if (error)
  781. return error;
  782. /*
  783. * Look up the given attribute in the leaf block. Figure out if
  784. * the given flags produce an error or call for an atomic rename.
  785. */
  786. retval = xfs_attr3_leaf_lookup_int(bp, args);
  787. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  788. xfs_trans_brelse(args->trans, bp);
  789. return retval;
  790. } else if (retval == EEXIST) {
  791. if (args->flags & ATTR_CREATE) { /* pure create op */
  792. xfs_trans_brelse(args->trans, bp);
  793. return retval;
  794. }
  795. trace_xfs_attr_leaf_replace(args);
  796. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  797. args->blkno2 = args->blkno; /* set 2nd entry info*/
  798. args->index2 = args->index;
  799. args->rmtblkno2 = args->rmtblkno;
  800. args->rmtblkcnt2 = args->rmtblkcnt;
  801. }
  802. /*
  803. * Add the attribute to the leaf block, transitioning to a Btree
  804. * if required.
  805. */
  806. retval = xfs_attr3_leaf_add(bp, args);
  807. if (retval == ENOSPC) {
  808. /*
  809. * Promote the attribute list to the Btree format, then
  810. * Commit that transaction so that the node_addname() call
  811. * can manage its own transactions.
  812. */
  813. xfs_bmap_init(args->flist, args->firstblock);
  814. error = xfs_attr3_leaf_to_node(args);
  815. if (!error) {
  816. error = xfs_bmap_finish(&args->trans, args->flist,
  817. &committed);
  818. }
  819. if (error) {
  820. ASSERT(committed);
  821. args->trans = NULL;
  822. xfs_bmap_cancel(args->flist);
  823. return(error);
  824. }
  825. /*
  826. * bmap_finish() may have committed the last trans and started
  827. * a new one. We need the inode to be in all transactions.
  828. */
  829. if (committed)
  830. xfs_trans_ijoin(args->trans, dp, 0);
  831. /*
  832. * Commit the current trans (including the inode) and start
  833. * a new one.
  834. */
  835. error = xfs_trans_roll(&args->trans, dp);
  836. if (error)
  837. return (error);
  838. /*
  839. * Fob the whole rest of the problem off on the Btree code.
  840. */
  841. error = xfs_attr_node_addname(args);
  842. return(error);
  843. }
  844. /*
  845. * Commit the transaction that added the attr name so that
  846. * later routines can manage their own transactions.
  847. */
  848. error = xfs_trans_roll(&args->trans, dp);
  849. if (error)
  850. return (error);
  851. /*
  852. * If there was an out-of-line value, allocate the blocks we
  853. * identified for its storage and copy the value. This is done
  854. * after we create the attribute so that we don't overflow the
  855. * maximum size of a transaction and/or hit a deadlock.
  856. */
  857. if (args->rmtblkno > 0) {
  858. error = xfs_attr_rmtval_set(args);
  859. if (error)
  860. return(error);
  861. }
  862. /*
  863. * If this is an atomic rename operation, we must "flip" the
  864. * incomplete flags on the "new" and "old" attribute/value pairs
  865. * so that one disappears and one appears atomically. Then we
  866. * must remove the "old" attribute/value pair.
  867. */
  868. if (args->op_flags & XFS_DA_OP_RENAME) {
  869. /*
  870. * In a separate transaction, set the incomplete flag on the
  871. * "old" attr and clear the incomplete flag on the "new" attr.
  872. */
  873. error = xfs_attr3_leaf_flipflags(args);
  874. if (error)
  875. return(error);
  876. /*
  877. * Dismantle the "old" attribute/value pair by removing
  878. * a "remote" value (if it exists).
  879. */
  880. args->index = args->index2;
  881. args->blkno = args->blkno2;
  882. args->rmtblkno = args->rmtblkno2;
  883. args->rmtblkcnt = args->rmtblkcnt2;
  884. if (args->rmtblkno) {
  885. error = xfs_attr_rmtval_remove(args);
  886. if (error)
  887. return(error);
  888. }
  889. /*
  890. * Read in the block containing the "old" attr, then
  891. * remove the "old" attr from that block (neat, huh!)
  892. */
  893. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  894. -1, &bp);
  895. if (error)
  896. return error;
  897. xfs_attr3_leaf_remove(bp, args);
  898. /*
  899. * If the result is small enough, shrink it all into the inode.
  900. */
  901. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  902. xfs_bmap_init(args->flist, args->firstblock);
  903. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  904. /* bp is gone due to xfs_da_shrink_inode */
  905. if (!error) {
  906. error = xfs_bmap_finish(&args->trans,
  907. args->flist,
  908. &committed);
  909. }
  910. if (error) {
  911. ASSERT(committed);
  912. args->trans = NULL;
  913. xfs_bmap_cancel(args->flist);
  914. return(error);
  915. }
  916. /*
  917. * bmap_finish() may have committed the last trans
  918. * and started a new one. We need the inode to be
  919. * in all transactions.
  920. */
  921. if (committed)
  922. xfs_trans_ijoin(args->trans, dp, 0);
  923. }
  924. /*
  925. * Commit the remove and start the next trans in series.
  926. */
  927. error = xfs_trans_roll(&args->trans, dp);
  928. } else if (args->rmtblkno > 0) {
  929. /*
  930. * Added a "remote" value, just clear the incomplete flag.
  931. */
  932. error = xfs_attr3_leaf_clearflag(args);
  933. }
  934. return error;
  935. }
  936. /*
  937. * Remove a name from the leaf attribute list structure
  938. *
  939. * This leaf block cannot have a "remote" value, we only call this routine
  940. * if bmap_one_block() says there is only one block (ie: no remote blks).
  941. */
  942. STATIC int
  943. xfs_attr_leaf_removename(xfs_da_args_t *args)
  944. {
  945. xfs_inode_t *dp;
  946. struct xfs_buf *bp;
  947. int error, committed, forkoff;
  948. trace_xfs_attr_leaf_removename(args);
  949. /*
  950. * Remove the attribute.
  951. */
  952. dp = args->dp;
  953. args->blkno = 0;
  954. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  955. if (error)
  956. return error;
  957. error = xfs_attr3_leaf_lookup_int(bp, args);
  958. if (error == ENOATTR) {
  959. xfs_trans_brelse(args->trans, bp);
  960. return error;
  961. }
  962. xfs_attr3_leaf_remove(bp, args);
  963. /*
  964. * If the result is small enough, shrink it all into the inode.
  965. */
  966. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  967. xfs_bmap_init(args->flist, args->firstblock);
  968. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  969. /* bp is gone due to xfs_da_shrink_inode */
  970. if (!error) {
  971. error = xfs_bmap_finish(&args->trans, args->flist,
  972. &committed);
  973. }
  974. if (error) {
  975. ASSERT(committed);
  976. args->trans = NULL;
  977. xfs_bmap_cancel(args->flist);
  978. return error;
  979. }
  980. /*
  981. * bmap_finish() may have committed the last trans and started
  982. * a new one. We need the inode to be in all transactions.
  983. */
  984. if (committed)
  985. xfs_trans_ijoin(args->trans, dp, 0);
  986. }
  987. return 0;
  988. }
  989. /*
  990. * Look up a name in a leaf attribute list structure.
  991. *
  992. * This leaf block cannot have a "remote" value, we only call this routine
  993. * if bmap_one_block() says there is only one block (ie: no remote blks).
  994. */
  995. STATIC int
  996. xfs_attr_leaf_get(xfs_da_args_t *args)
  997. {
  998. struct xfs_buf *bp;
  999. int error;
  1000. trace_xfs_attr_leaf_get(args);
  1001. args->blkno = 0;
  1002. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  1003. if (error)
  1004. return error;
  1005. error = xfs_attr3_leaf_lookup_int(bp, args);
  1006. if (error != EEXIST) {
  1007. xfs_trans_brelse(args->trans, bp);
  1008. return error;
  1009. }
  1010. error = xfs_attr3_leaf_getvalue(bp, args);
  1011. xfs_trans_brelse(args->trans, bp);
  1012. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  1013. error = xfs_attr_rmtval_get(args);
  1014. }
  1015. return error;
  1016. }
  1017. /*
  1018. * Copy out attribute entries for attr_list(), for leaf attribute lists.
  1019. */
  1020. STATIC int
  1021. xfs_attr_leaf_list(xfs_attr_list_context_t *context)
  1022. {
  1023. int error;
  1024. struct xfs_buf *bp;
  1025. trace_xfs_attr_leaf_list(context);
  1026. context->cursor->blkno = 0;
  1027. error = xfs_attr3_leaf_read(NULL, context->dp, 0, -1, &bp);
  1028. if (error)
  1029. return XFS_ERROR(error);
  1030. error = xfs_attr3_leaf_list_int(bp, context);
  1031. xfs_trans_brelse(NULL, bp);
  1032. return XFS_ERROR(error);
  1033. }
  1034. /*========================================================================
  1035. * External routines when attribute list size > XFS_LBSIZE(mp).
  1036. *========================================================================*/
  1037. /*
  1038. * Add a name to a Btree-format attribute list.
  1039. *
  1040. * This will involve walking down the Btree, and may involve splitting
  1041. * leaf nodes and even splitting intermediate nodes up to and including
  1042. * the root node (a special case of an intermediate node).
  1043. *
  1044. * "Remote" attribute values confuse the issue and atomic rename operations
  1045. * add a whole extra layer of confusion on top of that.
  1046. */
  1047. STATIC int
  1048. xfs_attr_node_addname(xfs_da_args_t *args)
  1049. {
  1050. xfs_da_state_t *state;
  1051. xfs_da_state_blk_t *blk;
  1052. xfs_inode_t *dp;
  1053. xfs_mount_t *mp;
  1054. int committed, retval, error;
  1055. trace_xfs_attr_node_addname(args);
  1056. /*
  1057. * Fill in bucket of arguments/results/context to carry around.
  1058. */
  1059. dp = args->dp;
  1060. mp = dp->i_mount;
  1061. restart:
  1062. state = xfs_da_state_alloc();
  1063. state->args = args;
  1064. state->mp = mp;
  1065. state->blocksize = state->mp->m_sb.sb_blocksize;
  1066. state->node_ents = state->mp->m_attr_node_ents;
  1067. /*
  1068. * Search to see if name already exists, and get back a pointer
  1069. * to where it should go.
  1070. */
  1071. error = xfs_da3_node_lookup_int(state, &retval);
  1072. if (error)
  1073. goto out;
  1074. blk = &state->path.blk[ state->path.active-1 ];
  1075. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1076. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  1077. goto out;
  1078. } else if (retval == EEXIST) {
  1079. if (args->flags & ATTR_CREATE)
  1080. goto out;
  1081. trace_xfs_attr_node_replace(args);
  1082. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  1083. args->blkno2 = args->blkno; /* set 2nd entry info*/
  1084. args->index2 = args->index;
  1085. args->rmtblkno2 = args->rmtblkno;
  1086. args->rmtblkcnt2 = args->rmtblkcnt;
  1087. args->rmtblkno = 0;
  1088. args->rmtblkcnt = 0;
  1089. }
  1090. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  1091. if (retval == ENOSPC) {
  1092. if (state->path.active == 1) {
  1093. /*
  1094. * Its really a single leaf node, but it had
  1095. * out-of-line values so it looked like it *might*
  1096. * have been a b-tree.
  1097. */
  1098. xfs_da_state_free(state);
  1099. xfs_bmap_init(args->flist, args->firstblock);
  1100. error = xfs_attr3_leaf_to_node(args);
  1101. if (!error) {
  1102. error = xfs_bmap_finish(&args->trans,
  1103. args->flist,
  1104. &committed);
  1105. }
  1106. if (error) {
  1107. ASSERT(committed);
  1108. args->trans = NULL;
  1109. xfs_bmap_cancel(args->flist);
  1110. goto out;
  1111. }
  1112. /*
  1113. * bmap_finish() may have committed the last trans
  1114. * and started a new one. We need the inode to be
  1115. * in all transactions.
  1116. */
  1117. if (committed)
  1118. xfs_trans_ijoin(args->trans, dp, 0);
  1119. /*
  1120. * Commit the node conversion and start the next
  1121. * trans in the chain.
  1122. */
  1123. error = xfs_trans_roll(&args->trans, dp);
  1124. if (error)
  1125. goto out;
  1126. goto restart;
  1127. }
  1128. /*
  1129. * Split as many Btree elements as required.
  1130. * This code tracks the new and old attr's location
  1131. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  1132. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  1133. */
  1134. xfs_bmap_init(args->flist, args->firstblock);
  1135. error = xfs_da3_split(state);
  1136. if (!error) {
  1137. error = xfs_bmap_finish(&args->trans, args->flist,
  1138. &committed);
  1139. }
  1140. if (error) {
  1141. ASSERT(committed);
  1142. args->trans = NULL;
  1143. xfs_bmap_cancel(args->flist);
  1144. goto out;
  1145. }
  1146. /*
  1147. * bmap_finish() may have committed the last trans and started
  1148. * a new one. We need the inode to be in all transactions.
  1149. */
  1150. if (committed)
  1151. xfs_trans_ijoin(args->trans, dp, 0);
  1152. } else {
  1153. /*
  1154. * Addition succeeded, update Btree hashvals.
  1155. */
  1156. xfs_da3_fixhashpath(state, &state->path);
  1157. }
  1158. /*
  1159. * Kill the state structure, we're done with it and need to
  1160. * allow the buffers to come back later.
  1161. */
  1162. xfs_da_state_free(state);
  1163. state = NULL;
  1164. /*
  1165. * Commit the leaf addition or btree split and start the next
  1166. * trans in the chain.
  1167. */
  1168. error = xfs_trans_roll(&args->trans, dp);
  1169. if (error)
  1170. goto out;
  1171. /*
  1172. * If there was an out-of-line value, allocate the blocks we
  1173. * identified for its storage and copy the value. This is done
  1174. * after we create the attribute so that we don't overflow the
  1175. * maximum size of a transaction and/or hit a deadlock.
  1176. */
  1177. if (args->rmtblkno > 0) {
  1178. error = xfs_attr_rmtval_set(args);
  1179. if (error)
  1180. return(error);
  1181. }
  1182. /*
  1183. * If this is an atomic rename operation, we must "flip" the
  1184. * incomplete flags on the "new" and "old" attribute/value pairs
  1185. * so that one disappears and one appears atomically. Then we
  1186. * must remove the "old" attribute/value pair.
  1187. */
  1188. if (args->op_flags & XFS_DA_OP_RENAME) {
  1189. /*
  1190. * In a separate transaction, set the incomplete flag on the
  1191. * "old" attr and clear the incomplete flag on the "new" attr.
  1192. */
  1193. error = xfs_attr3_leaf_flipflags(args);
  1194. if (error)
  1195. goto out;
  1196. /*
  1197. * Dismantle the "old" attribute/value pair by removing
  1198. * a "remote" value (if it exists).
  1199. */
  1200. args->index = args->index2;
  1201. args->blkno = args->blkno2;
  1202. args->rmtblkno = args->rmtblkno2;
  1203. args->rmtblkcnt = args->rmtblkcnt2;
  1204. if (args->rmtblkno) {
  1205. error = xfs_attr_rmtval_remove(args);
  1206. if (error)
  1207. return(error);
  1208. }
  1209. /*
  1210. * Re-find the "old" attribute entry after any split ops.
  1211. * The INCOMPLETE flag means that we will find the "old"
  1212. * attr, not the "new" one.
  1213. */
  1214. args->flags |= XFS_ATTR_INCOMPLETE;
  1215. state = xfs_da_state_alloc();
  1216. state->args = args;
  1217. state->mp = mp;
  1218. state->blocksize = state->mp->m_sb.sb_blocksize;
  1219. state->node_ents = state->mp->m_attr_node_ents;
  1220. state->inleaf = 0;
  1221. error = xfs_da3_node_lookup_int(state, &retval);
  1222. if (error)
  1223. goto out;
  1224. /*
  1225. * Remove the name and update the hashvals in the tree.
  1226. */
  1227. blk = &state->path.blk[ state->path.active-1 ];
  1228. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1229. error = xfs_attr3_leaf_remove(blk->bp, args);
  1230. xfs_da3_fixhashpath(state, &state->path);
  1231. /*
  1232. * Check to see if the tree needs to be collapsed.
  1233. */
  1234. if (retval && (state->path.active > 1)) {
  1235. xfs_bmap_init(args->flist, args->firstblock);
  1236. error = xfs_da3_join(state);
  1237. if (!error) {
  1238. error = xfs_bmap_finish(&args->trans,
  1239. args->flist,
  1240. &committed);
  1241. }
  1242. if (error) {
  1243. ASSERT(committed);
  1244. args->trans = NULL;
  1245. xfs_bmap_cancel(args->flist);
  1246. goto out;
  1247. }
  1248. /*
  1249. * bmap_finish() may have committed the last trans
  1250. * and started a new one. We need the inode to be
  1251. * in all transactions.
  1252. */
  1253. if (committed)
  1254. xfs_trans_ijoin(args->trans, dp, 0);
  1255. }
  1256. /*
  1257. * Commit and start the next trans in the chain.
  1258. */
  1259. error = xfs_trans_roll(&args->trans, dp);
  1260. if (error)
  1261. goto out;
  1262. } else if (args->rmtblkno > 0) {
  1263. /*
  1264. * Added a "remote" value, just clear the incomplete flag.
  1265. */
  1266. error = xfs_attr3_leaf_clearflag(args);
  1267. if (error)
  1268. goto out;
  1269. }
  1270. retval = error = 0;
  1271. out:
  1272. if (state)
  1273. xfs_da_state_free(state);
  1274. if (error)
  1275. return(error);
  1276. return(retval);
  1277. }
  1278. /*
  1279. * Remove a name from a B-tree attribute list.
  1280. *
  1281. * This will involve walking down the Btree, and may involve joining
  1282. * leaf nodes and even joining intermediate nodes up to and including
  1283. * the root node (a special case of an intermediate node).
  1284. */
  1285. STATIC int
  1286. xfs_attr_node_removename(xfs_da_args_t *args)
  1287. {
  1288. xfs_da_state_t *state;
  1289. xfs_da_state_blk_t *blk;
  1290. xfs_inode_t *dp;
  1291. struct xfs_buf *bp;
  1292. int retval, error, committed, forkoff;
  1293. trace_xfs_attr_node_removename(args);
  1294. /*
  1295. * Tie a string around our finger to remind us where we are.
  1296. */
  1297. dp = args->dp;
  1298. state = xfs_da_state_alloc();
  1299. state->args = args;
  1300. state->mp = dp->i_mount;
  1301. state->blocksize = state->mp->m_sb.sb_blocksize;
  1302. state->node_ents = state->mp->m_attr_node_ents;
  1303. /*
  1304. * Search to see if name exists, and get back a pointer to it.
  1305. */
  1306. error = xfs_da3_node_lookup_int(state, &retval);
  1307. if (error || (retval != EEXIST)) {
  1308. if (error == 0)
  1309. error = retval;
  1310. goto out;
  1311. }
  1312. /*
  1313. * If there is an out-of-line value, de-allocate the blocks.
  1314. * This is done before we remove the attribute so that we don't
  1315. * overflow the maximum size of a transaction and/or hit a deadlock.
  1316. */
  1317. blk = &state->path.blk[ state->path.active-1 ];
  1318. ASSERT(blk->bp != NULL);
  1319. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1320. if (args->rmtblkno > 0) {
  1321. /*
  1322. * Fill in disk block numbers in the state structure
  1323. * so that we can get the buffers back after we commit
  1324. * several transactions in the following calls.
  1325. */
  1326. error = xfs_attr_fillstate(state);
  1327. if (error)
  1328. goto out;
  1329. /*
  1330. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1331. * remote value.
  1332. */
  1333. error = xfs_attr3_leaf_setflag(args);
  1334. if (error)
  1335. goto out;
  1336. error = xfs_attr_rmtval_remove(args);
  1337. if (error)
  1338. goto out;
  1339. /*
  1340. * Refill the state structure with buffers, the prior calls
  1341. * released our buffers.
  1342. */
  1343. error = xfs_attr_refillstate(state);
  1344. if (error)
  1345. goto out;
  1346. }
  1347. /*
  1348. * Remove the name and update the hashvals in the tree.
  1349. */
  1350. blk = &state->path.blk[ state->path.active-1 ];
  1351. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1352. retval = xfs_attr3_leaf_remove(blk->bp, args);
  1353. xfs_da3_fixhashpath(state, &state->path);
  1354. /*
  1355. * Check to see if the tree needs to be collapsed.
  1356. */
  1357. if (retval && (state->path.active > 1)) {
  1358. xfs_bmap_init(args->flist, args->firstblock);
  1359. error = xfs_da3_join(state);
  1360. if (!error) {
  1361. error = xfs_bmap_finish(&args->trans, args->flist,
  1362. &committed);
  1363. }
  1364. if (error) {
  1365. ASSERT(committed);
  1366. args->trans = NULL;
  1367. xfs_bmap_cancel(args->flist);
  1368. goto out;
  1369. }
  1370. /*
  1371. * bmap_finish() may have committed the last trans and started
  1372. * a new one. We need the inode to be in all transactions.
  1373. */
  1374. if (committed)
  1375. xfs_trans_ijoin(args->trans, dp, 0);
  1376. /*
  1377. * Commit the Btree join operation and start a new trans.
  1378. */
  1379. error = xfs_trans_roll(&args->trans, dp);
  1380. if (error)
  1381. goto out;
  1382. }
  1383. /*
  1384. * If the result is small enough, push it all into the inode.
  1385. */
  1386. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1387. /*
  1388. * Have to get rid of the copy of this dabuf in the state.
  1389. */
  1390. ASSERT(state->path.active == 1);
  1391. ASSERT(state->path.blk[0].bp);
  1392. state->path.blk[0].bp = NULL;
  1393. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
  1394. if (error)
  1395. goto out;
  1396. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1397. xfs_bmap_init(args->flist, args->firstblock);
  1398. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1399. /* bp is gone due to xfs_da_shrink_inode */
  1400. if (!error) {
  1401. error = xfs_bmap_finish(&args->trans,
  1402. args->flist,
  1403. &committed);
  1404. }
  1405. if (error) {
  1406. ASSERT(committed);
  1407. args->trans = NULL;
  1408. xfs_bmap_cancel(args->flist);
  1409. goto out;
  1410. }
  1411. /*
  1412. * bmap_finish() may have committed the last trans
  1413. * and started a new one. We need the inode to be
  1414. * in all transactions.
  1415. */
  1416. if (committed)
  1417. xfs_trans_ijoin(args->trans, dp, 0);
  1418. } else
  1419. xfs_trans_brelse(args->trans, bp);
  1420. }
  1421. error = 0;
  1422. out:
  1423. xfs_da_state_free(state);
  1424. return(error);
  1425. }
  1426. /*
  1427. * Fill in the disk block numbers in the state structure for the buffers
  1428. * that are attached to the state structure.
  1429. * This is done so that we can quickly reattach ourselves to those buffers
  1430. * after some set of transaction commits have released these buffers.
  1431. */
  1432. STATIC int
  1433. xfs_attr_fillstate(xfs_da_state_t *state)
  1434. {
  1435. xfs_da_state_path_t *path;
  1436. xfs_da_state_blk_t *blk;
  1437. int level;
  1438. trace_xfs_attr_fillstate(state->args);
  1439. /*
  1440. * Roll down the "path" in the state structure, storing the on-disk
  1441. * block number for those buffers in the "path".
  1442. */
  1443. path = &state->path;
  1444. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1445. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1446. if (blk->bp) {
  1447. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1448. blk->bp = NULL;
  1449. } else {
  1450. blk->disk_blkno = 0;
  1451. }
  1452. }
  1453. /*
  1454. * Roll down the "altpath" in the state structure, storing the on-disk
  1455. * block number for those buffers in the "altpath".
  1456. */
  1457. path = &state->altpath;
  1458. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1459. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1460. if (blk->bp) {
  1461. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1462. blk->bp = NULL;
  1463. } else {
  1464. blk->disk_blkno = 0;
  1465. }
  1466. }
  1467. return(0);
  1468. }
  1469. /*
  1470. * Reattach the buffers to the state structure based on the disk block
  1471. * numbers stored in the state structure.
  1472. * This is done after some set of transaction commits have released those
  1473. * buffers from our grip.
  1474. */
  1475. STATIC int
  1476. xfs_attr_refillstate(xfs_da_state_t *state)
  1477. {
  1478. xfs_da_state_path_t *path;
  1479. xfs_da_state_blk_t *blk;
  1480. int level, error;
  1481. trace_xfs_attr_refillstate(state->args);
  1482. /*
  1483. * Roll down the "path" in the state structure, storing the on-disk
  1484. * block number for those buffers in the "path".
  1485. */
  1486. path = &state->path;
  1487. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1488. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1489. if (blk->disk_blkno) {
  1490. error = xfs_da3_node_read(state->args->trans,
  1491. state->args->dp,
  1492. blk->blkno, blk->disk_blkno,
  1493. &blk->bp, XFS_ATTR_FORK);
  1494. if (error)
  1495. return(error);
  1496. } else {
  1497. blk->bp = NULL;
  1498. }
  1499. }
  1500. /*
  1501. * Roll down the "altpath" in the state structure, storing the on-disk
  1502. * block number for those buffers in the "altpath".
  1503. */
  1504. path = &state->altpath;
  1505. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1506. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1507. if (blk->disk_blkno) {
  1508. error = xfs_da3_node_read(state->args->trans,
  1509. state->args->dp,
  1510. blk->blkno, blk->disk_blkno,
  1511. &blk->bp, XFS_ATTR_FORK);
  1512. if (error)
  1513. return(error);
  1514. } else {
  1515. blk->bp = NULL;
  1516. }
  1517. }
  1518. return(0);
  1519. }
  1520. /*
  1521. * Look up a filename in a node attribute list.
  1522. *
  1523. * This routine gets called for any attribute fork that has more than one
  1524. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1525. * "remote" values taking up more blocks.
  1526. */
  1527. STATIC int
  1528. xfs_attr_node_get(xfs_da_args_t *args)
  1529. {
  1530. xfs_da_state_t *state;
  1531. xfs_da_state_blk_t *blk;
  1532. int error, retval;
  1533. int i;
  1534. trace_xfs_attr_node_get(args);
  1535. state = xfs_da_state_alloc();
  1536. state->args = args;
  1537. state->mp = args->dp->i_mount;
  1538. state->blocksize = state->mp->m_sb.sb_blocksize;
  1539. state->node_ents = state->mp->m_attr_node_ents;
  1540. /*
  1541. * Search to see if name exists, and get back a pointer to it.
  1542. */
  1543. error = xfs_da3_node_lookup_int(state, &retval);
  1544. if (error) {
  1545. retval = error;
  1546. } else if (retval == EEXIST) {
  1547. blk = &state->path.blk[ state->path.active-1 ];
  1548. ASSERT(blk->bp != NULL);
  1549. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1550. /*
  1551. * Get the value, local or "remote"
  1552. */
  1553. retval = xfs_attr3_leaf_getvalue(blk->bp, args);
  1554. if (!retval && (args->rmtblkno > 0)
  1555. && !(args->flags & ATTR_KERNOVAL)) {
  1556. retval = xfs_attr_rmtval_get(args);
  1557. }
  1558. }
  1559. /*
  1560. * If not in a transaction, we have to release all the buffers.
  1561. */
  1562. for (i = 0; i < state->path.active; i++) {
  1563. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1564. state->path.blk[i].bp = NULL;
  1565. }
  1566. xfs_da_state_free(state);
  1567. return(retval);
  1568. }
  1569. STATIC int /* error */
  1570. xfs_attr_node_list(xfs_attr_list_context_t *context)
  1571. {
  1572. attrlist_cursor_kern_t *cursor;
  1573. xfs_attr_leafblock_t *leaf;
  1574. xfs_da_intnode_t *node;
  1575. struct xfs_attr3_icleaf_hdr leafhdr;
  1576. struct xfs_da3_icnode_hdr nodehdr;
  1577. struct xfs_da_node_entry *btree;
  1578. int error, i;
  1579. struct xfs_buf *bp;
  1580. trace_xfs_attr_node_list(context);
  1581. cursor = context->cursor;
  1582. cursor->initted = 1;
  1583. /*
  1584. * Do all sorts of validation on the passed-in cursor structure.
  1585. * If anything is amiss, ignore the cursor and look up the hashval
  1586. * starting from the btree root.
  1587. */
  1588. bp = NULL;
  1589. if (cursor->blkno > 0) {
  1590. error = xfs_da3_node_read(NULL, context->dp, cursor->blkno, -1,
  1591. &bp, XFS_ATTR_FORK);
  1592. if ((error != 0) && (error != EFSCORRUPTED))
  1593. return(error);
  1594. if (bp) {
  1595. struct xfs_attr_leaf_entry *entries;
  1596. node = bp->b_addr;
  1597. switch (be16_to_cpu(node->hdr.info.magic)) {
  1598. case XFS_DA_NODE_MAGIC:
  1599. case XFS_DA3_NODE_MAGIC:
  1600. trace_xfs_attr_list_wrong_blk(context);
  1601. xfs_trans_brelse(NULL, bp);
  1602. bp = NULL;
  1603. break;
  1604. case XFS_ATTR_LEAF_MAGIC:
  1605. case XFS_ATTR3_LEAF_MAGIC:
  1606. leaf = bp->b_addr;
  1607. xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
  1608. entries = xfs_attr3_leaf_entryp(leaf);
  1609. if (cursor->hashval > be32_to_cpu(
  1610. entries[leafhdr.count - 1].hashval)) {
  1611. trace_xfs_attr_list_wrong_blk(context);
  1612. xfs_trans_brelse(NULL, bp);
  1613. bp = NULL;
  1614. } else if (cursor->hashval <= be32_to_cpu(
  1615. entries[0].hashval)) {
  1616. trace_xfs_attr_list_wrong_blk(context);
  1617. xfs_trans_brelse(NULL, bp);
  1618. bp = NULL;
  1619. }
  1620. break;
  1621. default:
  1622. trace_xfs_attr_list_wrong_blk(context);
  1623. xfs_trans_brelse(NULL, bp);
  1624. bp = NULL;
  1625. }
  1626. }
  1627. }
  1628. /*
  1629. * We did not find what we expected given the cursor's contents,
  1630. * so we start from the top and work down based on the hash value.
  1631. * Note that start of node block is same as start of leaf block.
  1632. */
  1633. if (bp == NULL) {
  1634. cursor->blkno = 0;
  1635. for (;;) {
  1636. __uint16_t magic;
  1637. error = xfs_da3_node_read(NULL, context->dp,
  1638. cursor->blkno, -1, &bp,
  1639. XFS_ATTR_FORK);
  1640. if (error)
  1641. return(error);
  1642. node = bp->b_addr;
  1643. magic = be16_to_cpu(node->hdr.info.magic);
  1644. if (magic == XFS_ATTR_LEAF_MAGIC ||
  1645. magic == XFS_ATTR3_LEAF_MAGIC)
  1646. break;
  1647. if (magic != XFS_DA_NODE_MAGIC &&
  1648. magic != XFS_DA3_NODE_MAGIC) {
  1649. XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
  1650. XFS_ERRLEVEL_LOW,
  1651. context->dp->i_mount,
  1652. node);
  1653. xfs_trans_brelse(NULL, bp);
  1654. return XFS_ERROR(EFSCORRUPTED);
  1655. }
  1656. xfs_da3_node_hdr_from_disk(&nodehdr, node);
  1657. btree = xfs_da3_node_tree_p(node);
  1658. for (i = 0; i < nodehdr.count; btree++, i++) {
  1659. if (cursor->hashval
  1660. <= be32_to_cpu(btree->hashval)) {
  1661. cursor->blkno = be32_to_cpu(btree->before);
  1662. trace_xfs_attr_list_node_descend(context,
  1663. btree);
  1664. break;
  1665. }
  1666. }
  1667. if (i == nodehdr.count) {
  1668. xfs_trans_brelse(NULL, bp);
  1669. return 0;
  1670. }
  1671. xfs_trans_brelse(NULL, bp);
  1672. }
  1673. }
  1674. ASSERT(bp != NULL);
  1675. /*
  1676. * Roll upward through the blocks, processing each leaf block in
  1677. * order. As long as there is space in the result buffer, keep
  1678. * adding the information.
  1679. */
  1680. for (;;) {
  1681. leaf = bp->b_addr;
  1682. error = xfs_attr3_leaf_list_int(bp, context);
  1683. if (error) {
  1684. xfs_trans_brelse(NULL, bp);
  1685. return error;
  1686. }
  1687. xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
  1688. if (context->seen_enough || leafhdr.forw == 0)
  1689. break;
  1690. cursor->blkno = leafhdr.forw;
  1691. xfs_trans_brelse(NULL, bp);
  1692. error = xfs_attr3_leaf_read(NULL, context->dp, cursor->blkno, -1,
  1693. &bp);
  1694. if (error)
  1695. return error;
  1696. }
  1697. xfs_trans_brelse(NULL, bp);
  1698. return 0;
  1699. }