xfs_attr.c 59 KB

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