xfs_attr.c 70 KB

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