xfs_attr.c 67 KB

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