xfs_attr.c 57 KB

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