xfs_attr.c 59 KB

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