dir.c 47 KB

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