xfs_attr.c 70 KB

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