dir.c 52 KB

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