dir.c 49 KB

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