xfs_attr.c 70 KB

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