dir.c 46 KB

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