dir.c 47 KB

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