xfs_attr.c 70 KB

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