xfs_attr.c 54 KB

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