dir.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. /*
  10. * Implements Extendible Hashing as described in:
  11. * "Extendible Hashing" by Fagin, et al in
  12. * __ACM Trans. on Database Systems__, Sept 1979.
  13. *
  14. *
  15. * Here's the layout of dirents which is essentially the same as that of ext2
  16. * within a single block. The field de_name_len is the number of bytes
  17. * actually required for the name (no null terminator). The field de_rec_len
  18. * is the number of bytes allocated to the dirent. The offset of the next
  19. * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
  20. * deleted, the preceding dirent inherits its allocated space, ie
  21. * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
  22. * by adding de_rec_len to the current dirent, this essentially causes the
  23. * deleted dirent to get jumped over when iterating through all the dirents.
  24. *
  25. * When deleting the first dirent in a block, there is no previous dirent so
  26. * the field de_ino is set to zero to designate it as deleted. When allocating
  27. * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
  28. * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
  29. * dirent is allocated. Otherwise it must go through all the 'used' dirents
  30. * searching for one in which the amount of total space minus the amount of
  31. * used space will provide enough space for the new dirent.
  32. *
  33. * There are two types of blocks in which dirents reside. In a stuffed dinode,
  34. * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
  35. * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
  36. * beginning of the leaf block. The dirents reside in leaves when
  37. *
  38. * dip->i_diskflags & GFS2_DIF_EXHASH is true
  39. *
  40. * Otherwise, the dirents are "linear", within a single stuffed dinode block.
  41. *
  42. * When the dirents are in leaves, the actual contents of the directory file are
  43. * used as an array of 64-bit block pointers pointing to the leaf blocks. The
  44. * dirents are NOT in the directory file itself. There can be more than one
  45. * block pointer in the array that points to the same leaf. In fact, when a
  46. * directory is first converted from linear to exhash, all of the pointers
  47. * point to the same leaf.
  48. *
  49. * When a leaf is completely full, the size of the hash table can be
  50. * doubled unless it is already at the maximum size which is hard coded into
  51. * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
  52. * but never before the maximum hash table size has been reached.
  53. */
  54. #include <linux/slab.h>
  55. #include <linux/spinlock.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/sort.h>
  58. #include <linux/gfs2_ondisk.h>
  59. #include <linux/crc32.h>
  60. #include <linux/vmalloc.h>
  61. #include "gfs2.h"
  62. #include "incore.h"
  63. #include "dir.h"
  64. #include "glock.h"
  65. #include "inode.h"
  66. #include "meta_io.h"
  67. #include "quota.h"
  68. #include "rgrp.h"
  69. #include "trans.h"
  70. #include "bmap.h"
  71. #include "util.h"
  72. #define IS_LEAF 1 /* Hashed (leaf) directory */
  73. #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
  74. #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
  75. #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
  76. typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
  77. u64 leaf_no, void *data);
  78. typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
  79. const struct qstr *name, void *opaque);
  80. int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
  81. struct buffer_head **bhp)
  82. {
  83. struct buffer_head *bh;
  84. bh = gfs2_meta_new(ip->i_gl, block);
  85. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  86. gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
  87. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  88. *bhp = bh;
  89. return 0;
  90. }
  91. static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
  92. struct buffer_head **bhp)
  93. {
  94. struct buffer_head *bh;
  95. int error;
  96. error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
  97. if (error)
  98. return error;
  99. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
  100. brelse(bh);
  101. return -EIO;
  102. }
  103. *bhp = bh;
  104. return 0;
  105. }
  106. static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
  107. unsigned int offset, unsigned int size)
  108. {
  109. struct buffer_head *dibh;
  110. int error;
  111. error = gfs2_meta_inode_buffer(ip, &dibh);
  112. if (error)
  113. return error;
  114. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  115. memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
  116. if (ip->i_disksize < offset + size)
  117. ip->i_disksize = offset + size;
  118. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  119. gfs2_dinode_out(ip, dibh->b_data);
  120. brelse(dibh);
  121. return size;
  122. }
  123. /**
  124. * gfs2_dir_write_data - Write directory information to the inode
  125. * @ip: The GFS2 inode
  126. * @buf: The buffer containing information to be written
  127. * @offset: The file offset to start writing at
  128. * @size: The amount of data to write
  129. *
  130. * Returns: The number of bytes correctly written or error code
  131. */
  132. static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
  133. u64 offset, unsigned int size)
  134. {
  135. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  136. struct buffer_head *dibh;
  137. u64 lblock, dblock;
  138. u32 extlen = 0;
  139. unsigned int o;
  140. int copied = 0;
  141. int error = 0;
  142. int new = 0;
  143. if (!size)
  144. return 0;
  145. if (gfs2_is_stuffed(ip) &&
  146. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  147. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  148. size);
  149. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  150. return -EINVAL;
  151. if (gfs2_is_stuffed(ip)) {
  152. error = gfs2_unstuff_dinode(ip, NULL);
  153. if (error)
  154. return error;
  155. }
  156. lblock = offset;
  157. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  158. while (copied < size) {
  159. unsigned int amount;
  160. struct buffer_head *bh;
  161. amount = size - copied;
  162. if (amount > sdp->sd_sb.sb_bsize - o)
  163. amount = sdp->sd_sb.sb_bsize - o;
  164. if (!extlen) {
  165. new = 1;
  166. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  167. &dblock, &extlen);
  168. if (error)
  169. goto fail;
  170. error = -EIO;
  171. if (gfs2_assert_withdraw(sdp, dblock))
  172. goto fail;
  173. }
  174. if (amount == sdp->sd_jbsize || new)
  175. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  176. else
  177. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  178. if (error)
  179. goto fail;
  180. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  181. memcpy(bh->b_data + o, buf, amount);
  182. brelse(bh);
  183. buf += amount;
  184. copied += amount;
  185. lblock++;
  186. dblock++;
  187. extlen--;
  188. o = sizeof(struct gfs2_meta_header);
  189. }
  190. out:
  191. error = gfs2_meta_inode_buffer(ip, &dibh);
  192. if (error)
  193. return error;
  194. if (ip->i_disksize < offset + copied)
  195. ip->i_disksize = offset + copied;
  196. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  197. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  198. gfs2_dinode_out(ip, dibh->b_data);
  199. brelse(dibh);
  200. return copied;
  201. fail:
  202. if (copied)
  203. goto out;
  204. return error;
  205. }
  206. static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
  207. u64 offset, unsigned int size)
  208. {
  209. struct buffer_head *dibh;
  210. int error;
  211. error = gfs2_meta_inode_buffer(ip, &dibh);
  212. if (!error) {
  213. offset += sizeof(struct gfs2_dinode);
  214. memcpy(buf, dibh->b_data + offset, size);
  215. brelse(dibh);
  216. }
  217. return (error) ? error : size;
  218. }
  219. /**
  220. * gfs2_dir_read_data - Read a data from a directory inode
  221. * @ip: The GFS2 Inode
  222. * @buf: The buffer to place result into
  223. * @offset: File offset to begin jdata_readng from
  224. * @size: Amount of data to transfer
  225. *
  226. * Returns: The amount of data actually copied or the error
  227. */
  228. static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
  229. unsigned int size, unsigned ra)
  230. {
  231. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  232. u64 lblock, dblock;
  233. u32 extlen = 0;
  234. unsigned int o;
  235. int copied = 0;
  236. int error = 0;
  237. if (offset >= ip->i_disksize)
  238. return 0;
  239. if (offset + size > ip->i_disksize)
  240. size = ip->i_disksize - offset;
  241. if (!size)
  242. return 0;
  243. if (gfs2_is_stuffed(ip))
  244. return gfs2_dir_read_stuffed(ip, buf, offset, size);
  245. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  246. return -EINVAL;
  247. lblock = offset;
  248. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  249. while (copied < size) {
  250. unsigned int amount;
  251. struct buffer_head *bh;
  252. int new;
  253. amount = size - copied;
  254. if (amount > sdp->sd_sb.sb_bsize - o)
  255. amount = sdp->sd_sb.sb_bsize - o;
  256. if (!extlen) {
  257. new = 0;
  258. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  259. &dblock, &extlen);
  260. if (error || !dblock)
  261. goto fail;
  262. BUG_ON(extlen < 1);
  263. if (!ra)
  264. extlen = 1;
  265. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  266. } else {
  267. error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
  268. if (error)
  269. goto fail;
  270. }
  271. error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
  272. if (error) {
  273. brelse(bh);
  274. goto fail;
  275. }
  276. dblock++;
  277. extlen--;
  278. memcpy(buf, bh->b_data + o, amount);
  279. brelse(bh);
  280. buf += amount;
  281. copied += amount;
  282. lblock++;
  283. o = sizeof(struct gfs2_meta_header);
  284. }
  285. return copied;
  286. fail:
  287. return (copied) ? copied : error;
  288. }
  289. static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
  290. {
  291. return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
  292. }
  293. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  294. const struct qstr *name, int ret)
  295. {
  296. if (!gfs2_dirent_sentinel(dent) &&
  297. be32_to_cpu(dent->de_hash) == name->hash &&
  298. be16_to_cpu(dent->de_name_len) == name->len &&
  299. memcmp(dent+1, name->name, name->len) == 0)
  300. return ret;
  301. return 0;
  302. }
  303. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  304. const struct qstr *name,
  305. void *opaque)
  306. {
  307. return __gfs2_dirent_find(dent, name, 1);
  308. }
  309. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  310. const struct qstr *name,
  311. void *opaque)
  312. {
  313. return __gfs2_dirent_find(dent, name, 2);
  314. }
  315. /*
  316. * name->name holds ptr to start of block.
  317. * name->len holds size of block.
  318. */
  319. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  320. const struct qstr *name,
  321. void *opaque)
  322. {
  323. const char *start = name->name;
  324. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  325. if (name->len == (end - start))
  326. return 1;
  327. return 0;
  328. }
  329. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  330. const struct qstr *name,
  331. void *opaque)
  332. {
  333. unsigned required = GFS2_DIRENT_SIZE(name->len);
  334. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  335. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  336. if (gfs2_dirent_sentinel(dent))
  337. actual = GFS2_DIRENT_SIZE(0);
  338. if (totlen - actual >= required)
  339. return 1;
  340. return 0;
  341. }
  342. struct dirent_gather {
  343. const struct gfs2_dirent **pdent;
  344. unsigned offset;
  345. };
  346. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  347. const struct qstr *name,
  348. void *opaque)
  349. {
  350. struct dirent_gather *g = opaque;
  351. if (!gfs2_dirent_sentinel(dent)) {
  352. g->pdent[g->offset++] = dent;
  353. }
  354. return 0;
  355. }
  356. /*
  357. * Other possible things to check:
  358. * - Inode located within filesystem size (and on valid block)
  359. * - Valid directory entry type
  360. * Not sure how heavy-weight we want to make this... could also check
  361. * hash is correct for example, but that would take a lot of extra time.
  362. * For now the most important thing is to check that the various sizes
  363. * are correct.
  364. */
  365. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  366. unsigned int size, unsigned int len, int first)
  367. {
  368. const char *msg = "gfs2_dirent too small";
  369. if (unlikely(size < sizeof(struct gfs2_dirent)))
  370. goto error;
  371. msg = "gfs2_dirent misaligned";
  372. if (unlikely(offset & 0x7))
  373. goto error;
  374. msg = "gfs2_dirent points beyond end of block";
  375. if (unlikely(offset + size > len))
  376. goto error;
  377. msg = "zero inode number";
  378. if (unlikely(!first && gfs2_dirent_sentinel(dent)))
  379. goto error;
  380. msg = "name length is greater than space in dirent";
  381. if (!gfs2_dirent_sentinel(dent) &&
  382. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  383. size))
  384. goto error;
  385. return 0;
  386. error:
  387. printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
  388. first ? "first in block" : "not first in block");
  389. return -EIO;
  390. }
  391. static int gfs2_dirent_offset(const void *buf)
  392. {
  393. const struct gfs2_meta_header *h = buf;
  394. int offset;
  395. BUG_ON(buf == NULL);
  396. switch(be32_to_cpu(h->mh_type)) {
  397. case GFS2_METATYPE_LF:
  398. offset = sizeof(struct gfs2_leaf);
  399. break;
  400. case GFS2_METATYPE_DI:
  401. offset = sizeof(struct gfs2_dinode);
  402. break;
  403. default:
  404. goto wrong_type;
  405. }
  406. return offset;
  407. wrong_type:
  408. printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
  409. be32_to_cpu(h->mh_type));
  410. return -1;
  411. }
  412. static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
  413. unsigned int len, gfs2_dscan_t scan,
  414. const struct qstr *name,
  415. void *opaque)
  416. {
  417. struct gfs2_dirent *dent, *prev;
  418. unsigned offset;
  419. unsigned size;
  420. int ret = 0;
  421. ret = gfs2_dirent_offset(buf);
  422. if (ret < 0)
  423. goto consist_inode;
  424. offset = ret;
  425. prev = NULL;
  426. dent = buf + offset;
  427. size = be16_to_cpu(dent->de_rec_len);
  428. if (gfs2_check_dirent(dent, offset, size, len, 1))
  429. goto consist_inode;
  430. do {
  431. ret = scan(dent, name, opaque);
  432. if (ret)
  433. break;
  434. offset += size;
  435. if (offset == len)
  436. break;
  437. prev = dent;
  438. dent = buf + offset;
  439. size = be16_to_cpu(dent->de_rec_len);
  440. if (gfs2_check_dirent(dent, offset, size, len, 0))
  441. goto consist_inode;
  442. } while(1);
  443. switch(ret) {
  444. case 0:
  445. return NULL;
  446. case 1:
  447. return dent;
  448. case 2:
  449. return prev ? prev : dent;
  450. default:
  451. BUG_ON(ret > 0);
  452. return ERR_PTR(ret);
  453. }
  454. consist_inode:
  455. gfs2_consist_inode(GFS2_I(inode));
  456. return ERR_PTR(-EIO);
  457. }
  458. static int dirent_check_reclen(struct gfs2_inode *dip,
  459. const struct gfs2_dirent *d, const void *end_p)
  460. {
  461. const void *ptr = d;
  462. u16 rec_len = be16_to_cpu(d->de_rec_len);
  463. if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
  464. goto broken;
  465. ptr += rec_len;
  466. if (ptr < end_p)
  467. return rec_len;
  468. if (ptr == end_p)
  469. return -ENOENT;
  470. broken:
  471. gfs2_consist_inode(dip);
  472. return -EIO;
  473. }
  474. /**
  475. * dirent_next - Next dirent
  476. * @dip: the directory
  477. * @bh: The buffer
  478. * @dent: Pointer to list of dirents
  479. *
  480. * Returns: 0 on success, error code otherwise
  481. */
  482. static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
  483. struct gfs2_dirent **dent)
  484. {
  485. struct gfs2_dirent *cur = *dent, *tmp;
  486. char *bh_end = bh->b_data + bh->b_size;
  487. int ret;
  488. ret = dirent_check_reclen(dip, cur, bh_end);
  489. if (ret < 0)
  490. return ret;
  491. tmp = (void *)cur + ret;
  492. ret = dirent_check_reclen(dip, tmp, bh_end);
  493. if (ret == -EIO)
  494. return ret;
  495. /* Only the first dent could ever have de_inum.no_addr == 0 */
  496. if (gfs2_dirent_sentinel(tmp)) {
  497. gfs2_consist_inode(dip);
  498. return -EIO;
  499. }
  500. *dent = tmp;
  501. return 0;
  502. }
  503. /**
  504. * dirent_del - Delete a dirent
  505. * @dip: The GFS2 inode
  506. * @bh: The buffer
  507. * @prev: The previous dirent
  508. * @cur: The current dirent
  509. *
  510. */
  511. static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
  512. struct gfs2_dirent *prev, struct gfs2_dirent *cur)
  513. {
  514. u16 cur_rec_len, prev_rec_len;
  515. if (gfs2_dirent_sentinel(cur)) {
  516. gfs2_consist_inode(dip);
  517. return;
  518. }
  519. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  520. /* If there is no prev entry, this is the first entry in the block.
  521. The de_rec_len is already as big as it needs to be. Just zero
  522. out the inode number and return. */
  523. if (!prev) {
  524. cur->de_inum.no_addr = 0;
  525. cur->de_inum.no_formal_ino = 0;
  526. return;
  527. }
  528. /* Combine this dentry with the previous one. */
  529. prev_rec_len = be16_to_cpu(prev->de_rec_len);
  530. cur_rec_len = be16_to_cpu(cur->de_rec_len);
  531. if ((char *)prev + prev_rec_len != (char *)cur)
  532. gfs2_consist_inode(dip);
  533. if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
  534. gfs2_consist_inode(dip);
  535. prev_rec_len += cur_rec_len;
  536. prev->de_rec_len = cpu_to_be16(prev_rec_len);
  537. }
  538. /*
  539. * Takes a dent from which to grab space as an argument. Returns the
  540. * newly created dent.
  541. */
  542. static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
  543. struct gfs2_dirent *dent,
  544. const struct qstr *name,
  545. struct buffer_head *bh)
  546. {
  547. struct gfs2_inode *ip = GFS2_I(inode);
  548. struct gfs2_dirent *ndent;
  549. unsigned offset = 0, totlen;
  550. if (!gfs2_dirent_sentinel(dent))
  551. offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  552. totlen = be16_to_cpu(dent->de_rec_len);
  553. BUG_ON(offset + name->len > totlen);
  554. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  555. ndent = (struct gfs2_dirent *)((char *)dent + offset);
  556. dent->de_rec_len = cpu_to_be16(offset);
  557. gfs2_qstr2dirent(name, totlen - offset, ndent);
  558. return ndent;
  559. }
  560. static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
  561. struct buffer_head *bh,
  562. const struct qstr *name)
  563. {
  564. struct gfs2_dirent *dent;
  565. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  566. gfs2_dirent_find_space, name, NULL);
  567. if (!dent || IS_ERR(dent))
  568. return dent;
  569. return gfs2_init_dirent(inode, dent, name, bh);
  570. }
  571. static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
  572. struct buffer_head **bhp)
  573. {
  574. int error;
  575. error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
  576. if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
  577. /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
  578. error = -EIO;
  579. }
  580. return error;
  581. }
  582. /**
  583. * get_leaf_nr - Get a leaf number associated with the index
  584. * @dip: The GFS2 inode
  585. * @index:
  586. * @leaf_out:
  587. *
  588. * Returns: 0 on success, error code otherwise
  589. */
  590. static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
  591. u64 *leaf_out)
  592. {
  593. __be64 leaf_no;
  594. int error;
  595. error = gfs2_dir_read_data(dip, (char *)&leaf_no,
  596. index * sizeof(__be64),
  597. sizeof(__be64), 0);
  598. if (error != sizeof(u64))
  599. return (error < 0) ? error : -EIO;
  600. *leaf_out = be64_to_cpu(leaf_no);
  601. return 0;
  602. }
  603. static int get_first_leaf(struct gfs2_inode *dip, u32 index,
  604. struct buffer_head **bh_out)
  605. {
  606. u64 leaf_no;
  607. int error;
  608. error = get_leaf_nr(dip, index, &leaf_no);
  609. if (!error)
  610. error = get_leaf(dip, leaf_no, bh_out);
  611. return error;
  612. }
  613. static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
  614. const struct qstr *name,
  615. gfs2_dscan_t scan,
  616. struct buffer_head **pbh)
  617. {
  618. struct buffer_head *bh;
  619. struct gfs2_dirent *dent;
  620. struct gfs2_inode *ip = GFS2_I(inode);
  621. int error;
  622. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  623. struct gfs2_leaf *leaf;
  624. unsigned hsize = 1 << ip->i_depth;
  625. unsigned index;
  626. u64 ln;
  627. if (hsize * sizeof(u64) != ip->i_disksize) {
  628. gfs2_consist_inode(ip);
  629. return ERR_PTR(-EIO);
  630. }
  631. index = name->hash >> (32 - ip->i_depth);
  632. error = get_first_leaf(ip, index, &bh);
  633. if (error)
  634. return ERR_PTR(error);
  635. do {
  636. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  637. scan, name, NULL);
  638. if (dent)
  639. goto got_dent;
  640. leaf = (struct gfs2_leaf *)bh->b_data;
  641. ln = be64_to_cpu(leaf->lf_next);
  642. brelse(bh);
  643. if (!ln)
  644. break;
  645. error = get_leaf(ip, ln, &bh);
  646. } while(!error);
  647. return error ? ERR_PTR(error) : NULL;
  648. }
  649. error = gfs2_meta_inode_buffer(ip, &bh);
  650. if (error)
  651. return ERR_PTR(error);
  652. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
  653. got_dent:
  654. if (unlikely(dent == NULL || IS_ERR(dent))) {
  655. brelse(bh);
  656. bh = NULL;
  657. }
  658. *pbh = bh;
  659. return dent;
  660. }
  661. static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
  662. {
  663. struct gfs2_inode *ip = GFS2_I(inode);
  664. unsigned int n = 1;
  665. u64 bn;
  666. int error;
  667. struct buffer_head *bh;
  668. struct gfs2_leaf *leaf;
  669. struct gfs2_dirent *dent;
  670. struct qstr name = { .name = "", .len = 0, .hash = 0 };
  671. error = gfs2_alloc_block(ip, &bn, &n);
  672. if (error)
  673. return NULL;
  674. bh = gfs2_meta_new(ip->i_gl, bn);
  675. if (!bh)
  676. return NULL;
  677. gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
  678. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  679. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  680. leaf = (struct gfs2_leaf *)bh->b_data;
  681. leaf->lf_depth = cpu_to_be16(depth);
  682. leaf->lf_entries = 0;
  683. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  684. leaf->lf_next = 0;
  685. memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
  686. dent = (struct gfs2_dirent *)(leaf+1);
  687. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  688. *pbh = bh;
  689. return leaf;
  690. }
  691. /**
  692. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  693. * @dip: The GFS2 inode
  694. *
  695. * Returns: 0 on success, error code otherwise
  696. */
  697. static int dir_make_exhash(struct inode *inode)
  698. {
  699. struct gfs2_inode *dip = GFS2_I(inode);
  700. struct gfs2_sbd *sdp = GFS2_SB(inode);
  701. struct gfs2_dirent *dent;
  702. struct qstr args;
  703. struct buffer_head *bh, *dibh;
  704. struct gfs2_leaf *leaf;
  705. int y;
  706. u32 x;
  707. __be64 *lp;
  708. u64 bn;
  709. int error;
  710. error = gfs2_meta_inode_buffer(dip, &dibh);
  711. if (error)
  712. return error;
  713. /* Turn over a new leaf */
  714. leaf = new_leaf(inode, &bh, 0);
  715. if (!leaf)
  716. return -ENOSPC;
  717. bn = bh->b_blocknr;
  718. gfs2_assert(sdp, dip->i_entries < (1 << 16));
  719. leaf->lf_entries = cpu_to_be16(dip->i_entries);
  720. /* Copy dirents */
  721. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  722. sizeof(struct gfs2_dinode));
  723. /* Find last entry */
  724. x = 0;
  725. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  726. sizeof(struct gfs2_leaf);
  727. args.name = bh->b_data;
  728. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  729. gfs2_dirent_last, &args, NULL);
  730. if (!dent) {
  731. brelse(bh);
  732. brelse(dibh);
  733. return -EIO;
  734. }
  735. if (IS_ERR(dent)) {
  736. brelse(bh);
  737. brelse(dibh);
  738. return PTR_ERR(dent);
  739. }
  740. /* Adjust the last dirent's record length
  741. (Remember that dent still points to the last entry.) */
  742. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  743. sizeof(struct gfs2_dinode) -
  744. sizeof(struct gfs2_leaf));
  745. brelse(bh);
  746. /* We're done with the new leaf block, now setup the new
  747. hash table. */
  748. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  749. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  750. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  751. for (x = sdp->sd_hash_ptrs; x--; lp++)
  752. *lp = cpu_to_be64(bn);
  753. dip->i_disksize = sdp->sd_sb.sb_bsize / 2;
  754. gfs2_add_inode_blocks(&dip->i_inode, 1);
  755. dip->i_diskflags |= GFS2_DIF_EXHASH;
  756. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  757. dip->i_depth = y;
  758. gfs2_dinode_out(dip, dibh->b_data);
  759. brelse(dibh);
  760. return 0;
  761. }
  762. /**
  763. * dir_split_leaf - Split a leaf block into two
  764. * @dip: The GFS2 inode
  765. * @index:
  766. * @leaf_no:
  767. *
  768. * Returns: 0 on success, error code on failure
  769. */
  770. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  771. {
  772. struct gfs2_inode *dip = GFS2_I(inode);
  773. struct buffer_head *nbh, *obh, *dibh;
  774. struct gfs2_leaf *nleaf, *oleaf;
  775. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  776. u32 start, len, half_len, divider;
  777. u64 bn, leaf_no;
  778. __be64 *lp;
  779. u32 index;
  780. int x, moved = 0;
  781. int error;
  782. index = name->hash >> (32 - dip->i_depth);
  783. error = get_leaf_nr(dip, index, &leaf_no);
  784. if (error)
  785. return error;
  786. /* Get the old leaf block */
  787. error = get_leaf(dip, leaf_no, &obh);
  788. if (error)
  789. return error;
  790. oleaf = (struct gfs2_leaf *)obh->b_data;
  791. if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
  792. brelse(obh);
  793. return 1; /* can't split */
  794. }
  795. gfs2_trans_add_bh(dip->i_gl, obh, 1);
  796. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  797. if (!nleaf) {
  798. brelse(obh);
  799. return -ENOSPC;
  800. }
  801. bn = nbh->b_blocknr;
  802. /* Compute the start and len of leaf pointers in the hash table. */
  803. len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
  804. half_len = len >> 1;
  805. if (!half_len) {
  806. printk(KERN_WARNING "i_depth %u lf_depth %u index %u\n", dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
  807. gfs2_consist_inode(dip);
  808. error = -EIO;
  809. goto fail_brelse;
  810. }
  811. start = (index & ~(len - 1));
  812. /* Change the pointers.
  813. Don't bother distinguishing stuffed from non-stuffed.
  814. This code is complicated enough already. */
  815. lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS | __GFP_NOFAIL);
  816. /* Change the pointers */
  817. for (x = 0; x < half_len; x++)
  818. lp[x] = cpu_to_be64(bn);
  819. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  820. half_len * sizeof(u64));
  821. if (error != half_len * sizeof(u64)) {
  822. if (error >= 0)
  823. error = -EIO;
  824. goto fail_lpfree;
  825. }
  826. kfree(lp);
  827. /* Compute the divider */
  828. divider = (start + half_len) << (32 - dip->i_depth);
  829. /* Copy the entries */
  830. dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
  831. do {
  832. next = dent;
  833. if (dirent_next(dip, obh, &next))
  834. next = NULL;
  835. if (!gfs2_dirent_sentinel(dent) &&
  836. be32_to_cpu(dent->de_hash) < divider) {
  837. struct qstr str;
  838. str.name = (char*)(dent+1);
  839. str.len = be16_to_cpu(dent->de_name_len);
  840. str.hash = be32_to_cpu(dent->de_hash);
  841. new = gfs2_dirent_alloc(inode, nbh, &str);
  842. if (IS_ERR(new)) {
  843. error = PTR_ERR(new);
  844. break;
  845. }
  846. new->de_inum = dent->de_inum; /* No endian worries */
  847. new->de_type = dent->de_type; /* No endian worries */
  848. be16_add_cpu(&nleaf->lf_entries, 1);
  849. dirent_del(dip, obh, prev, dent);
  850. if (!oleaf->lf_entries)
  851. gfs2_consist_inode(dip);
  852. be16_add_cpu(&oleaf->lf_entries, -1);
  853. if (!prev)
  854. prev = dent;
  855. moved = 1;
  856. } else {
  857. prev = dent;
  858. }
  859. dent = next;
  860. } while (dent);
  861. oleaf->lf_depth = nleaf->lf_depth;
  862. error = gfs2_meta_inode_buffer(dip, &dibh);
  863. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  864. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  865. gfs2_add_inode_blocks(&dip->i_inode, 1);
  866. gfs2_dinode_out(dip, dibh->b_data);
  867. brelse(dibh);
  868. }
  869. brelse(obh);
  870. brelse(nbh);
  871. return error;
  872. fail_lpfree:
  873. kfree(lp);
  874. fail_brelse:
  875. brelse(obh);
  876. brelse(nbh);
  877. return error;
  878. }
  879. /**
  880. * dir_double_exhash - Double size of ExHash table
  881. * @dip: The GFS2 dinode
  882. *
  883. * Returns: 0 on success, error code on failure
  884. */
  885. static int dir_double_exhash(struct gfs2_inode *dip)
  886. {
  887. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  888. struct buffer_head *dibh;
  889. u32 hsize;
  890. u64 *buf;
  891. u64 *from, *to;
  892. u64 block;
  893. int x;
  894. int error = 0;
  895. hsize = 1 << dip->i_depth;
  896. if (hsize * sizeof(u64) != dip->i_disksize) {
  897. gfs2_consist_inode(dip);
  898. return -EIO;
  899. }
  900. /* Allocate both the "from" and "to" buffers in one big chunk */
  901. buf = kcalloc(3, sdp->sd_hash_bsize, GFP_NOFS | __GFP_NOFAIL);
  902. for (block = dip->i_disksize >> sdp->sd_hash_bsize_shift; block--;) {
  903. error = gfs2_dir_read_data(dip, (char *)buf,
  904. block * sdp->sd_hash_bsize,
  905. sdp->sd_hash_bsize, 1);
  906. if (error != sdp->sd_hash_bsize) {
  907. if (error >= 0)
  908. error = -EIO;
  909. goto fail;
  910. }
  911. from = buf;
  912. to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
  913. for (x = sdp->sd_hash_ptrs; x--; from++) {
  914. *to++ = *from; /* No endianess worries */
  915. *to++ = *from;
  916. }
  917. error = gfs2_dir_write_data(dip,
  918. (char *)buf + sdp->sd_hash_bsize,
  919. block * sdp->sd_sb.sb_bsize,
  920. sdp->sd_sb.sb_bsize);
  921. if (error != sdp->sd_sb.sb_bsize) {
  922. if (error >= 0)
  923. error = -EIO;
  924. goto fail;
  925. }
  926. }
  927. kfree(buf);
  928. error = gfs2_meta_inode_buffer(dip, &dibh);
  929. if (!gfs2_assert_withdraw(sdp, !error)) {
  930. dip->i_depth++;
  931. gfs2_dinode_out(dip, dibh->b_data);
  932. brelse(dibh);
  933. }
  934. return error;
  935. fail:
  936. kfree(buf);
  937. return error;
  938. }
  939. /**
  940. * compare_dents - compare directory entries by hash value
  941. * @a: first dent
  942. * @b: second dent
  943. *
  944. * When comparing the hash entries of @a to @b:
  945. * gt: returns 1
  946. * lt: returns -1
  947. * eq: returns 0
  948. */
  949. static int compare_dents(const void *a, const void *b)
  950. {
  951. const struct gfs2_dirent *dent_a, *dent_b;
  952. u32 hash_a, hash_b;
  953. int ret = 0;
  954. dent_a = *(const struct gfs2_dirent **)a;
  955. hash_a = be32_to_cpu(dent_a->de_hash);
  956. dent_b = *(const struct gfs2_dirent **)b;
  957. hash_b = be32_to_cpu(dent_b->de_hash);
  958. if (hash_a > hash_b)
  959. ret = 1;
  960. else if (hash_a < hash_b)
  961. ret = -1;
  962. else {
  963. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  964. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  965. if (len_a > len_b)
  966. ret = 1;
  967. else if (len_a < len_b)
  968. ret = -1;
  969. else
  970. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  971. }
  972. return ret;
  973. }
  974. /**
  975. * do_filldir_main - read out directory entries
  976. * @dip: The GFS2 inode
  977. * @offset: The offset in the file to read from
  978. * @opaque: opaque data to pass to filldir
  979. * @filldir: The function to pass entries to
  980. * @darr: an array of struct gfs2_dirent pointers to read
  981. * @entries: the number of entries in darr
  982. * @copied: pointer to int that's non-zero if a entry has been copied out
  983. *
  984. * Jump through some hoops to make sure that if there are hash collsions,
  985. * they are read out at the beginning of a buffer. We want to minimize
  986. * the possibility that they will fall into different readdir buffers or
  987. * that someone will want to seek to that location.
  988. *
  989. * Returns: errno, >0 on exception from filldir
  990. */
  991. static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
  992. void *opaque, filldir_t filldir,
  993. const struct gfs2_dirent **darr, u32 entries,
  994. int *copied)
  995. {
  996. const struct gfs2_dirent *dent, *dent_next;
  997. u64 off, off_next;
  998. unsigned int x, y;
  999. int run = 0;
  1000. int error = 0;
  1001. sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1002. dent_next = darr[0];
  1003. off_next = be32_to_cpu(dent_next->de_hash);
  1004. off_next = gfs2_disk_hash2offset(off_next);
  1005. for (x = 0, y = 1; x < entries; x++, y++) {
  1006. dent = dent_next;
  1007. off = off_next;
  1008. if (y < entries) {
  1009. dent_next = darr[y];
  1010. off_next = be32_to_cpu(dent_next->de_hash);
  1011. off_next = gfs2_disk_hash2offset(off_next);
  1012. if (off < *offset)
  1013. continue;
  1014. *offset = off;
  1015. if (off_next == off) {
  1016. if (*copied && !run)
  1017. return 1;
  1018. run = 1;
  1019. } else
  1020. run = 0;
  1021. } else {
  1022. if (off < *offset)
  1023. continue;
  1024. *offset = off;
  1025. }
  1026. error = filldir(opaque, (const char *)(dent + 1),
  1027. be16_to_cpu(dent->de_name_len),
  1028. off, be64_to_cpu(dent->de_inum.no_addr),
  1029. be16_to_cpu(dent->de_type));
  1030. if (error)
  1031. return 1;
  1032. *copied = 1;
  1033. }
  1034. /* Increment the *offset by one, so the next time we come into the
  1035. do_filldir fxn, we get the next entry instead of the last one in the
  1036. current leaf */
  1037. (*offset)++;
  1038. return 0;
  1039. }
  1040. static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
  1041. filldir_t filldir, int *copied, unsigned *depth,
  1042. u64 leaf_no)
  1043. {
  1044. struct gfs2_inode *ip = GFS2_I(inode);
  1045. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1046. struct buffer_head *bh;
  1047. struct gfs2_leaf *lf;
  1048. unsigned entries = 0, entries2 = 0;
  1049. unsigned leaves = 0;
  1050. const struct gfs2_dirent **darr, *dent;
  1051. struct dirent_gather g;
  1052. struct buffer_head **larr;
  1053. int leaf = 0;
  1054. int error, i;
  1055. u64 lfn = leaf_no;
  1056. do {
  1057. error = get_leaf(ip, lfn, &bh);
  1058. if (error)
  1059. goto out;
  1060. lf = (struct gfs2_leaf *)bh->b_data;
  1061. if (leaves == 0)
  1062. *depth = be16_to_cpu(lf->lf_depth);
  1063. entries += be16_to_cpu(lf->lf_entries);
  1064. leaves++;
  1065. lfn = be64_to_cpu(lf->lf_next);
  1066. brelse(bh);
  1067. } while(lfn);
  1068. if (!entries)
  1069. return 0;
  1070. error = -ENOMEM;
  1071. /*
  1072. * The extra 99 entries are not normally used, but are a buffer
  1073. * zone in case the number of entries in the leaf is corrupt.
  1074. * 99 is the maximum number of entries that can fit in a single
  1075. * leaf block.
  1076. */
  1077. larr = vmalloc((leaves + entries + 99) * sizeof(void *));
  1078. if (!larr)
  1079. goto out;
  1080. darr = (const struct gfs2_dirent **)(larr + leaves);
  1081. g.pdent = darr;
  1082. g.offset = 0;
  1083. lfn = leaf_no;
  1084. do {
  1085. error = get_leaf(ip, lfn, &bh);
  1086. if (error)
  1087. goto out_kfree;
  1088. lf = (struct gfs2_leaf *)bh->b_data;
  1089. lfn = be64_to_cpu(lf->lf_next);
  1090. if (lf->lf_entries) {
  1091. entries2 += be16_to_cpu(lf->lf_entries);
  1092. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1093. gfs2_dirent_gather, NULL, &g);
  1094. error = PTR_ERR(dent);
  1095. if (IS_ERR(dent))
  1096. goto out_kfree;
  1097. if (entries2 != g.offset) {
  1098. fs_warn(sdp, "Number of entries corrupt in dir "
  1099. "leaf %llu, entries2 (%u) != "
  1100. "g.offset (%u)\n",
  1101. (unsigned long long)bh->b_blocknr,
  1102. entries2, g.offset);
  1103. error = -EIO;
  1104. goto out_kfree;
  1105. }
  1106. error = 0;
  1107. larr[leaf++] = bh;
  1108. } else {
  1109. brelse(bh);
  1110. }
  1111. } while(lfn);
  1112. BUG_ON(entries2 != entries);
  1113. error = do_filldir_main(ip, offset, opaque, filldir, darr,
  1114. entries, copied);
  1115. out_kfree:
  1116. for(i = 0; i < leaf; i++)
  1117. brelse(larr[i]);
  1118. vfree(larr);
  1119. out:
  1120. return error;
  1121. }
  1122. /**
  1123. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1124. * @dip: dinode pointer
  1125. * @offset: the hash of the last entry read shifted to the right once
  1126. * @opaque: buffer for the filldir function to fill
  1127. * @filldir: points to the filldir function to use
  1128. *
  1129. * Returns: errno
  1130. */
  1131. static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
  1132. filldir_t filldir)
  1133. {
  1134. struct gfs2_inode *dip = GFS2_I(inode);
  1135. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1136. u32 hsize, len = 0;
  1137. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1138. u32 hash, index;
  1139. __be64 *lp;
  1140. int copied = 0;
  1141. int error = 0;
  1142. unsigned depth = 0;
  1143. hsize = 1 << dip->i_depth;
  1144. if (hsize * sizeof(u64) != dip->i_disksize) {
  1145. gfs2_consist_inode(dip);
  1146. return -EIO;
  1147. }
  1148. hash = gfs2_dir_offset2hash(*offset);
  1149. index = hash >> (32 - dip->i_depth);
  1150. lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
  1151. if (!lp)
  1152. return -ENOMEM;
  1153. while (index < hsize) {
  1154. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1155. ht_offset = index - lp_offset;
  1156. if (ht_offset_cur != ht_offset) {
  1157. error = gfs2_dir_read_data(dip, (char *)lp,
  1158. ht_offset * sizeof(__be64),
  1159. sdp->sd_hash_bsize, 1);
  1160. if (error != sdp->sd_hash_bsize) {
  1161. if (error >= 0)
  1162. error = -EIO;
  1163. goto out;
  1164. }
  1165. ht_offset_cur = ht_offset;
  1166. }
  1167. error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
  1168. &copied, &depth,
  1169. be64_to_cpu(lp[lp_offset]));
  1170. if (error)
  1171. break;
  1172. len = 1 << (dip->i_depth - depth);
  1173. index = (index & ~(len - 1)) + len;
  1174. }
  1175. out:
  1176. kfree(lp);
  1177. if (error > 0)
  1178. error = 0;
  1179. return error;
  1180. }
  1181. int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
  1182. filldir_t filldir)
  1183. {
  1184. struct gfs2_inode *dip = GFS2_I(inode);
  1185. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1186. struct dirent_gather g;
  1187. const struct gfs2_dirent **darr, *dent;
  1188. struct buffer_head *dibh;
  1189. int copied = 0;
  1190. int error;
  1191. if (!dip->i_entries)
  1192. return 0;
  1193. if (dip->i_diskflags & GFS2_DIF_EXHASH)
  1194. return dir_e_read(inode, offset, opaque, filldir);
  1195. if (!gfs2_is_stuffed(dip)) {
  1196. gfs2_consist_inode(dip);
  1197. return -EIO;
  1198. }
  1199. error = gfs2_meta_inode_buffer(dip, &dibh);
  1200. if (error)
  1201. return error;
  1202. error = -ENOMEM;
  1203. /* 96 is max number of dirents which can be stuffed into an inode */
  1204. darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
  1205. if (darr) {
  1206. g.pdent = darr;
  1207. g.offset = 0;
  1208. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1209. gfs2_dirent_gather, NULL, &g);
  1210. if (IS_ERR(dent)) {
  1211. error = PTR_ERR(dent);
  1212. goto out;
  1213. }
  1214. if (dip->i_entries != g.offset) {
  1215. fs_warn(sdp, "Number of entries corrupt in dir %llu, "
  1216. "ip->i_entries (%u) != g.offset (%u)\n",
  1217. (unsigned long long)dip->i_no_addr,
  1218. dip->i_entries,
  1219. g.offset);
  1220. error = -EIO;
  1221. goto out;
  1222. }
  1223. error = do_filldir_main(dip, offset, opaque, filldir, darr,
  1224. dip->i_entries, &copied);
  1225. out:
  1226. kfree(darr);
  1227. }
  1228. if (error > 0)
  1229. error = 0;
  1230. brelse(dibh);
  1231. return error;
  1232. }
  1233. /**
  1234. * gfs2_dir_search - Search a directory
  1235. * @dip: The GFS2 inode
  1236. * @filename:
  1237. * @inode:
  1238. *
  1239. * This routine searches a directory for a file or another directory.
  1240. * Assumes a glock is held on dip.
  1241. *
  1242. * Returns: errno
  1243. */
  1244. struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
  1245. {
  1246. struct buffer_head *bh;
  1247. struct gfs2_dirent *dent;
  1248. struct inode *inode;
  1249. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1250. if (dent) {
  1251. if (IS_ERR(dent))
  1252. return ERR_CAST(dent);
  1253. inode = gfs2_inode_lookup(dir->i_sb,
  1254. be16_to_cpu(dent->de_type),
  1255. be64_to_cpu(dent->de_inum.no_addr),
  1256. be64_to_cpu(dent->de_inum.no_formal_ino), 0);
  1257. brelse(bh);
  1258. return inode;
  1259. }
  1260. return ERR_PTR(-ENOENT);
  1261. }
  1262. int gfs2_dir_check(struct inode *dir, const struct qstr *name,
  1263. const struct gfs2_inode *ip)
  1264. {
  1265. struct buffer_head *bh;
  1266. struct gfs2_dirent *dent;
  1267. int ret = -ENOENT;
  1268. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1269. if (dent) {
  1270. if (IS_ERR(dent))
  1271. return PTR_ERR(dent);
  1272. if (ip) {
  1273. if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
  1274. goto out;
  1275. if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
  1276. ip->i_no_formal_ino)
  1277. goto out;
  1278. if (unlikely(IF2DT(ip->i_inode.i_mode) !=
  1279. be16_to_cpu(dent->de_type))) {
  1280. gfs2_consist_inode(GFS2_I(dir));
  1281. ret = -EIO;
  1282. goto out;
  1283. }
  1284. }
  1285. ret = 0;
  1286. out:
  1287. brelse(bh);
  1288. }
  1289. return ret;
  1290. }
  1291. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1292. {
  1293. struct buffer_head *bh, *obh;
  1294. struct gfs2_inode *ip = GFS2_I(inode);
  1295. struct gfs2_leaf *leaf, *oleaf;
  1296. int error;
  1297. u32 index;
  1298. u64 bn;
  1299. index = name->hash >> (32 - ip->i_depth);
  1300. error = get_first_leaf(ip, index, &obh);
  1301. if (error)
  1302. return error;
  1303. do {
  1304. oleaf = (struct gfs2_leaf *)obh->b_data;
  1305. bn = be64_to_cpu(oleaf->lf_next);
  1306. if (!bn)
  1307. break;
  1308. brelse(obh);
  1309. error = get_leaf(ip, bn, &obh);
  1310. if (error)
  1311. return error;
  1312. } while(1);
  1313. gfs2_trans_add_bh(ip->i_gl, obh, 1);
  1314. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1315. if (!leaf) {
  1316. brelse(obh);
  1317. return -ENOSPC;
  1318. }
  1319. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1320. brelse(bh);
  1321. brelse(obh);
  1322. error = gfs2_meta_inode_buffer(ip, &bh);
  1323. if (error)
  1324. return error;
  1325. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1326. gfs2_add_inode_blocks(&ip->i_inode, 1);
  1327. gfs2_dinode_out(ip, bh->b_data);
  1328. brelse(bh);
  1329. return 0;
  1330. }
  1331. /**
  1332. * gfs2_dir_add - Add new filename into directory
  1333. * @dip: The GFS2 inode
  1334. * @filename: The new name
  1335. * @inode: The inode number of the entry
  1336. * @type: The type of the entry
  1337. *
  1338. * Returns: 0 on success, error code on failure
  1339. */
  1340. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1341. const struct gfs2_inode *nip, unsigned type)
  1342. {
  1343. struct gfs2_inode *ip = GFS2_I(inode);
  1344. struct buffer_head *bh;
  1345. struct gfs2_dirent *dent;
  1346. struct gfs2_leaf *leaf;
  1347. int error;
  1348. while(1) {
  1349. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
  1350. &bh);
  1351. if (dent) {
  1352. if (IS_ERR(dent))
  1353. return PTR_ERR(dent);
  1354. dent = gfs2_init_dirent(inode, dent, name, bh);
  1355. gfs2_inum_out(nip, dent);
  1356. dent->de_type = cpu_to_be16(type);
  1357. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  1358. leaf = (struct gfs2_leaf *)bh->b_data;
  1359. be16_add_cpu(&leaf->lf_entries, 1);
  1360. }
  1361. brelse(bh);
  1362. error = gfs2_meta_inode_buffer(ip, &bh);
  1363. if (error)
  1364. break;
  1365. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1366. ip->i_entries++;
  1367. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1368. gfs2_dinode_out(ip, bh->b_data);
  1369. brelse(bh);
  1370. error = 0;
  1371. break;
  1372. }
  1373. if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1374. error = dir_make_exhash(inode);
  1375. if (error)
  1376. break;
  1377. continue;
  1378. }
  1379. error = dir_split_leaf(inode, name);
  1380. if (error == 0)
  1381. continue;
  1382. if (error < 0)
  1383. break;
  1384. if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
  1385. error = dir_double_exhash(ip);
  1386. if (error)
  1387. break;
  1388. error = dir_split_leaf(inode, name);
  1389. if (error < 0)
  1390. break;
  1391. if (error == 0)
  1392. continue;
  1393. }
  1394. error = dir_new_leaf(inode, name);
  1395. if (!error)
  1396. continue;
  1397. error = -ENOSPC;
  1398. break;
  1399. }
  1400. return error;
  1401. }
  1402. /**
  1403. * gfs2_dir_del - Delete a directory entry
  1404. * @dip: The GFS2 inode
  1405. * @filename: The filename
  1406. *
  1407. * Returns: 0 on success, error code on failure
  1408. */
  1409. int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
  1410. {
  1411. struct gfs2_dirent *dent, *prev = NULL;
  1412. struct buffer_head *bh;
  1413. int error;
  1414. /* Returns _either_ the entry (if its first in block) or the
  1415. previous entry otherwise */
  1416. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1417. if (!dent) {
  1418. gfs2_consist_inode(dip);
  1419. return -EIO;
  1420. }
  1421. if (IS_ERR(dent)) {
  1422. gfs2_consist_inode(dip);
  1423. return PTR_ERR(dent);
  1424. }
  1425. /* If not first in block, adjust pointers accordingly */
  1426. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1427. prev = dent;
  1428. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1429. }
  1430. dirent_del(dip, bh, prev, dent);
  1431. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1432. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1433. u16 entries = be16_to_cpu(leaf->lf_entries);
  1434. if (!entries)
  1435. gfs2_consist_inode(dip);
  1436. leaf->lf_entries = cpu_to_be16(--entries);
  1437. }
  1438. brelse(bh);
  1439. error = gfs2_meta_inode_buffer(dip, &bh);
  1440. if (error)
  1441. return error;
  1442. if (!dip->i_entries)
  1443. gfs2_consist_inode(dip);
  1444. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1445. dip->i_entries--;
  1446. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1447. gfs2_dinode_out(dip, bh->b_data);
  1448. brelse(bh);
  1449. mark_inode_dirty(&dip->i_inode);
  1450. return error;
  1451. }
  1452. /**
  1453. * gfs2_dir_mvino - Change inode number of directory entry
  1454. * @dip: The GFS2 inode
  1455. * @filename:
  1456. * @new_inode:
  1457. *
  1458. * This routine changes the inode number of a directory entry. It's used
  1459. * by rename to change ".." when a directory is moved.
  1460. * Assumes a glock is held on dvp.
  1461. *
  1462. * Returns: errno
  1463. */
  1464. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1465. const struct gfs2_inode *nip, unsigned int new_type)
  1466. {
  1467. struct buffer_head *bh;
  1468. struct gfs2_dirent *dent;
  1469. int error;
  1470. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1471. if (!dent) {
  1472. gfs2_consist_inode(dip);
  1473. return -EIO;
  1474. }
  1475. if (IS_ERR(dent))
  1476. return PTR_ERR(dent);
  1477. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1478. gfs2_inum_out(nip, dent);
  1479. dent->de_type = cpu_to_be16(new_type);
  1480. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1481. brelse(bh);
  1482. error = gfs2_meta_inode_buffer(dip, &bh);
  1483. if (error)
  1484. return error;
  1485. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1486. }
  1487. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1488. gfs2_dinode_out(dip, bh->b_data);
  1489. brelse(bh);
  1490. return 0;
  1491. }
  1492. /**
  1493. * foreach_leaf - call a function for each leaf in a directory
  1494. * @dip: the directory
  1495. * @lc: the function to call for each each
  1496. * @data: private data to pass to it
  1497. *
  1498. * Returns: errno
  1499. */
  1500. static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
  1501. {
  1502. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1503. struct buffer_head *bh;
  1504. struct gfs2_leaf *leaf;
  1505. u32 hsize, len;
  1506. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1507. u32 index = 0;
  1508. __be64 *lp;
  1509. u64 leaf_no;
  1510. int error = 0;
  1511. hsize = 1 << dip->i_depth;
  1512. if (hsize * sizeof(u64) != dip->i_disksize) {
  1513. gfs2_consist_inode(dip);
  1514. return -EIO;
  1515. }
  1516. lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
  1517. if (!lp)
  1518. return -ENOMEM;
  1519. while (index < hsize) {
  1520. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1521. ht_offset = index - lp_offset;
  1522. if (ht_offset_cur != ht_offset) {
  1523. error = gfs2_dir_read_data(dip, (char *)lp,
  1524. ht_offset * sizeof(__be64),
  1525. sdp->sd_hash_bsize, 1);
  1526. if (error != sdp->sd_hash_bsize) {
  1527. if (error >= 0)
  1528. error = -EIO;
  1529. goto out;
  1530. }
  1531. ht_offset_cur = ht_offset;
  1532. }
  1533. leaf_no = be64_to_cpu(lp[lp_offset]);
  1534. if (leaf_no) {
  1535. error = get_leaf(dip, leaf_no, &bh);
  1536. if (error)
  1537. goto out;
  1538. leaf = (struct gfs2_leaf *)bh->b_data;
  1539. len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
  1540. brelse(bh);
  1541. error = lc(dip, index, len, leaf_no, data);
  1542. if (error)
  1543. goto out;
  1544. index = (index & ~(len - 1)) + len;
  1545. } else
  1546. index++;
  1547. }
  1548. if (index != hsize) {
  1549. gfs2_consist_inode(dip);
  1550. error = -EIO;
  1551. }
  1552. out:
  1553. kfree(lp);
  1554. return error;
  1555. }
  1556. /**
  1557. * leaf_dealloc - Deallocate a directory leaf
  1558. * @dip: the directory
  1559. * @index: the hash table offset in the directory
  1560. * @len: the number of pointers to this leaf
  1561. * @leaf_no: the leaf number
  1562. * @data: not used
  1563. *
  1564. * Returns: errno
  1565. */
  1566. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1567. u64 leaf_no, void *data)
  1568. {
  1569. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1570. struct gfs2_leaf *tmp_leaf;
  1571. struct gfs2_rgrp_list rlist;
  1572. struct buffer_head *bh, *dibh;
  1573. u64 blk, nblk;
  1574. unsigned int rg_blocks = 0, l_blocks = 0;
  1575. char *ht;
  1576. unsigned int x, size = len * sizeof(u64);
  1577. int error;
  1578. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1579. ht = kzalloc(size, GFP_NOFS);
  1580. if (!ht)
  1581. return -ENOMEM;
  1582. if (!gfs2_alloc_get(dip)) {
  1583. error = -ENOMEM;
  1584. goto out;
  1585. }
  1586. error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1587. if (error)
  1588. goto out_put;
  1589. error = gfs2_rindex_hold(sdp, &dip->i_alloc->al_ri_gh);
  1590. if (error)
  1591. goto out_qs;
  1592. /* Count the number of leaves */
  1593. for (blk = leaf_no; blk; blk = nblk) {
  1594. error = get_leaf(dip, blk, &bh);
  1595. if (error)
  1596. goto out_rlist;
  1597. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1598. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1599. brelse(bh);
  1600. gfs2_rlist_add(sdp, &rlist, blk);
  1601. l_blocks++;
  1602. }
  1603. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1604. for (x = 0; x < rlist.rl_rgrps; x++) {
  1605. struct gfs2_rgrpd *rgd;
  1606. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1607. rg_blocks += rgd->rd_length;
  1608. }
  1609. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1610. if (error)
  1611. goto out_rlist;
  1612. error = gfs2_trans_begin(sdp,
  1613. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1614. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1615. if (error)
  1616. goto out_rg_gunlock;
  1617. for (blk = leaf_no; blk; blk = nblk) {
  1618. error = get_leaf(dip, blk, &bh);
  1619. if (error)
  1620. goto out_end_trans;
  1621. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1622. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1623. brelse(bh);
  1624. gfs2_free_meta(dip, blk, 1);
  1625. gfs2_add_inode_blocks(&dip->i_inode, -1);
  1626. }
  1627. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1628. if (error != size) {
  1629. if (error >= 0)
  1630. error = -EIO;
  1631. goto out_end_trans;
  1632. }
  1633. error = gfs2_meta_inode_buffer(dip, &dibh);
  1634. if (error)
  1635. goto out_end_trans;
  1636. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1637. gfs2_dinode_out(dip, dibh->b_data);
  1638. brelse(dibh);
  1639. out_end_trans:
  1640. gfs2_trans_end(sdp);
  1641. out_rg_gunlock:
  1642. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1643. out_rlist:
  1644. gfs2_rlist_free(&rlist);
  1645. gfs2_glock_dq_uninit(&dip->i_alloc->al_ri_gh);
  1646. out_qs:
  1647. gfs2_quota_unhold(dip);
  1648. out_put:
  1649. gfs2_alloc_put(dip);
  1650. out:
  1651. kfree(ht);
  1652. return error;
  1653. }
  1654. /**
  1655. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1656. * @dip: the directory
  1657. *
  1658. * Dealloc all on-disk directory leaves to FREEMETA state
  1659. * Change on-disk inode type to "regular file"
  1660. *
  1661. * Returns: errno
  1662. */
  1663. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1664. {
  1665. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1666. struct buffer_head *bh;
  1667. int error;
  1668. /* Dealloc on-disk leaves to FREEMETA state */
  1669. error = foreach_leaf(dip, leaf_dealloc, NULL);
  1670. if (error)
  1671. return error;
  1672. /* Make this a regular file in case we crash.
  1673. (We don't want to free these blocks a second time.) */
  1674. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1675. if (error)
  1676. return error;
  1677. error = gfs2_meta_inode_buffer(dip, &bh);
  1678. if (!error) {
  1679. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1680. ((struct gfs2_dinode *)bh->b_data)->di_mode =
  1681. cpu_to_be32(S_IFREG);
  1682. brelse(bh);
  1683. }
  1684. gfs2_trans_end(sdp);
  1685. return error;
  1686. }
  1687. /**
  1688. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1689. * @ip: the file being written to
  1690. * @filname: the filename that's going to be added
  1691. *
  1692. * Returns: 1 if alloc required, 0 if not, -ve on error
  1693. */
  1694. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
  1695. {
  1696. struct gfs2_dirent *dent;
  1697. struct buffer_head *bh;
  1698. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1699. if (!dent) {
  1700. return 1;
  1701. }
  1702. if (IS_ERR(dent))
  1703. return PTR_ERR(dent);
  1704. brelse(bh);
  1705. return 0;
  1706. }