xfs_attr.c 56 KB

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