xfs_attr.c 67 KB

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