xfs_attr.c 57 KB

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