dir.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  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. hash = gfs2_dir_get_hash_table(dip);
  674. if (IS_ERR(hash))
  675. return PTR_ERR(hash);
  676. *leaf_out = be64_to_cpu(*(hash + index));
  677. return 0;
  678. }
  679. static int get_first_leaf(struct gfs2_inode *dip, u32 index,
  680. struct buffer_head **bh_out)
  681. {
  682. u64 leaf_no;
  683. int error;
  684. error = get_leaf_nr(dip, index, &leaf_no);
  685. if (!error)
  686. error = get_leaf(dip, leaf_no, bh_out);
  687. return error;
  688. }
  689. static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
  690. const struct qstr *name,
  691. gfs2_dscan_t scan,
  692. struct buffer_head **pbh)
  693. {
  694. struct buffer_head *bh;
  695. struct gfs2_dirent *dent;
  696. struct gfs2_inode *ip = GFS2_I(inode);
  697. int error;
  698. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  699. struct gfs2_leaf *leaf;
  700. unsigned hsize = 1 << ip->i_depth;
  701. unsigned index;
  702. u64 ln;
  703. if (hsize * sizeof(u64) != i_size_read(inode)) {
  704. gfs2_consist_inode(ip);
  705. return ERR_PTR(-EIO);
  706. }
  707. index = name->hash >> (32 - ip->i_depth);
  708. error = get_first_leaf(ip, index, &bh);
  709. if (error)
  710. return ERR_PTR(error);
  711. do {
  712. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  713. scan, name, NULL);
  714. if (dent)
  715. goto got_dent;
  716. leaf = (struct gfs2_leaf *)bh->b_data;
  717. ln = be64_to_cpu(leaf->lf_next);
  718. brelse(bh);
  719. if (!ln)
  720. break;
  721. error = get_leaf(ip, ln, &bh);
  722. } while(!error);
  723. return error ? ERR_PTR(error) : NULL;
  724. }
  725. error = gfs2_meta_inode_buffer(ip, &bh);
  726. if (error)
  727. return ERR_PTR(error);
  728. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
  729. got_dent:
  730. if (unlikely(dent == NULL || IS_ERR(dent))) {
  731. brelse(bh);
  732. bh = NULL;
  733. }
  734. *pbh = bh;
  735. return dent;
  736. }
  737. static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
  738. {
  739. struct gfs2_inode *ip = GFS2_I(inode);
  740. unsigned int n = 1;
  741. u64 bn;
  742. int error;
  743. struct buffer_head *bh;
  744. struct gfs2_leaf *leaf;
  745. struct gfs2_dirent *dent;
  746. struct qstr name = { .name = "" };
  747. struct timespec tv = CURRENT_TIME;
  748. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  749. if (error)
  750. return NULL;
  751. bh = gfs2_meta_new(ip->i_gl, bn);
  752. if (!bh)
  753. return NULL;
  754. gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
  755. gfs2_trans_add_meta(ip->i_gl, bh);
  756. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  757. leaf = (struct gfs2_leaf *)bh->b_data;
  758. leaf->lf_depth = cpu_to_be16(depth);
  759. leaf->lf_entries = 0;
  760. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  761. leaf->lf_next = 0;
  762. leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
  763. leaf->lf_dist = cpu_to_be32(1);
  764. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  765. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  766. memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
  767. dent = (struct gfs2_dirent *)(leaf+1);
  768. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  769. *pbh = bh;
  770. return leaf;
  771. }
  772. /**
  773. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  774. * @dip: The GFS2 inode
  775. *
  776. * Returns: 0 on success, error code otherwise
  777. */
  778. static int dir_make_exhash(struct inode *inode)
  779. {
  780. struct gfs2_inode *dip = GFS2_I(inode);
  781. struct gfs2_sbd *sdp = GFS2_SB(inode);
  782. struct gfs2_dirent *dent;
  783. struct qstr args;
  784. struct buffer_head *bh, *dibh;
  785. struct gfs2_leaf *leaf;
  786. int y;
  787. u32 x;
  788. __be64 *lp;
  789. u64 bn;
  790. int error;
  791. error = gfs2_meta_inode_buffer(dip, &dibh);
  792. if (error)
  793. return error;
  794. /* Turn over a new leaf */
  795. leaf = new_leaf(inode, &bh, 0);
  796. if (!leaf)
  797. return -ENOSPC;
  798. bn = bh->b_blocknr;
  799. gfs2_assert(sdp, dip->i_entries < (1 << 16));
  800. leaf->lf_entries = cpu_to_be16(dip->i_entries);
  801. /* Copy dirents */
  802. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  803. sizeof(struct gfs2_dinode));
  804. /* Find last entry */
  805. x = 0;
  806. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  807. sizeof(struct gfs2_leaf);
  808. args.name = bh->b_data;
  809. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  810. gfs2_dirent_last, &args, NULL);
  811. if (!dent) {
  812. brelse(bh);
  813. brelse(dibh);
  814. return -EIO;
  815. }
  816. if (IS_ERR(dent)) {
  817. brelse(bh);
  818. brelse(dibh);
  819. return PTR_ERR(dent);
  820. }
  821. /* Adjust the last dirent's record length
  822. (Remember that dent still points to the last entry.) */
  823. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  824. sizeof(struct gfs2_dinode) -
  825. sizeof(struct gfs2_leaf));
  826. brelse(bh);
  827. /* We're done with the new leaf block, now setup the new
  828. hash table. */
  829. gfs2_trans_add_meta(dip->i_gl, dibh);
  830. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  831. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  832. for (x = sdp->sd_hash_ptrs; x--; lp++)
  833. *lp = cpu_to_be64(bn);
  834. i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
  835. gfs2_add_inode_blocks(&dip->i_inode, 1);
  836. dip->i_diskflags |= GFS2_DIF_EXHASH;
  837. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  838. dip->i_depth = y;
  839. gfs2_dinode_out(dip, dibh->b_data);
  840. brelse(dibh);
  841. return 0;
  842. }
  843. /**
  844. * dir_split_leaf - Split a leaf block into two
  845. * @dip: The GFS2 inode
  846. * @index:
  847. * @leaf_no:
  848. *
  849. * Returns: 0 on success, error code on failure
  850. */
  851. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  852. {
  853. struct gfs2_inode *dip = GFS2_I(inode);
  854. struct buffer_head *nbh, *obh, *dibh;
  855. struct gfs2_leaf *nleaf, *oleaf;
  856. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  857. u32 start, len, half_len, divider;
  858. u64 bn, leaf_no;
  859. __be64 *lp;
  860. u32 index;
  861. int x, moved = 0;
  862. int error;
  863. index = name->hash >> (32 - dip->i_depth);
  864. error = get_leaf_nr(dip, index, &leaf_no);
  865. if (error)
  866. return error;
  867. /* Get the old leaf block */
  868. error = get_leaf(dip, leaf_no, &obh);
  869. if (error)
  870. return error;
  871. oleaf = (struct gfs2_leaf *)obh->b_data;
  872. if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
  873. brelse(obh);
  874. return 1; /* can't split */
  875. }
  876. gfs2_trans_add_meta(dip->i_gl, obh);
  877. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  878. if (!nleaf) {
  879. brelse(obh);
  880. return -ENOSPC;
  881. }
  882. bn = nbh->b_blocknr;
  883. /* Compute the start and len of leaf pointers in the hash table. */
  884. len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
  885. half_len = len >> 1;
  886. if (!half_len) {
  887. pr_warn("i_depth %u lf_depth %u index %u\n",
  888. dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
  889. gfs2_consist_inode(dip);
  890. error = -EIO;
  891. goto fail_brelse;
  892. }
  893. start = (index & ~(len - 1));
  894. /* Change the pointers.
  895. Don't bother distinguishing stuffed from non-stuffed.
  896. This code is complicated enough already. */
  897. lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
  898. if (!lp) {
  899. error = -ENOMEM;
  900. goto fail_brelse;
  901. }
  902. /* Change the pointers */
  903. for (x = 0; x < half_len; x++)
  904. lp[x] = cpu_to_be64(bn);
  905. gfs2_dir_hash_inval(dip);
  906. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  907. half_len * sizeof(u64));
  908. if (error != half_len * sizeof(u64)) {
  909. if (error >= 0)
  910. error = -EIO;
  911. goto fail_lpfree;
  912. }
  913. kfree(lp);
  914. /* Compute the divider */
  915. divider = (start + half_len) << (32 - dip->i_depth);
  916. /* Copy the entries */
  917. dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
  918. do {
  919. next = dent;
  920. if (dirent_next(dip, obh, &next))
  921. next = NULL;
  922. if (!gfs2_dirent_sentinel(dent) &&
  923. be32_to_cpu(dent->de_hash) < divider) {
  924. struct qstr str;
  925. void *ptr = ((char *)dent - obh->b_data) + nbh->b_data;
  926. str.name = (char*)(dent+1);
  927. str.len = be16_to_cpu(dent->de_name_len);
  928. str.hash = be32_to_cpu(dent->de_hash);
  929. new = gfs2_dirent_split_alloc(inode, nbh, &str, ptr);
  930. if (IS_ERR(new)) {
  931. error = PTR_ERR(new);
  932. break;
  933. }
  934. new->de_inum = dent->de_inum; /* No endian worries */
  935. new->de_type = dent->de_type; /* No endian worries */
  936. be16_add_cpu(&nleaf->lf_entries, 1);
  937. dirent_del(dip, obh, prev, dent);
  938. if (!oleaf->lf_entries)
  939. gfs2_consist_inode(dip);
  940. be16_add_cpu(&oleaf->lf_entries, -1);
  941. if (!prev)
  942. prev = dent;
  943. moved = 1;
  944. } else {
  945. prev = dent;
  946. }
  947. dent = next;
  948. } while (dent);
  949. oleaf->lf_depth = nleaf->lf_depth;
  950. error = gfs2_meta_inode_buffer(dip, &dibh);
  951. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  952. gfs2_trans_add_meta(dip->i_gl, dibh);
  953. gfs2_add_inode_blocks(&dip->i_inode, 1);
  954. gfs2_dinode_out(dip, dibh->b_data);
  955. brelse(dibh);
  956. }
  957. brelse(obh);
  958. brelse(nbh);
  959. return error;
  960. fail_lpfree:
  961. kfree(lp);
  962. fail_brelse:
  963. brelse(obh);
  964. brelse(nbh);
  965. return error;
  966. }
  967. /**
  968. * dir_double_exhash - Double size of ExHash table
  969. * @dip: The GFS2 dinode
  970. *
  971. * Returns: 0 on success, error code on failure
  972. */
  973. static int dir_double_exhash(struct gfs2_inode *dip)
  974. {
  975. struct buffer_head *dibh;
  976. u32 hsize;
  977. u32 hsize_bytes;
  978. __be64 *hc;
  979. __be64 *hc2, *h;
  980. int x;
  981. int error = 0;
  982. hsize = 1 << dip->i_depth;
  983. hsize_bytes = hsize * sizeof(__be64);
  984. hc = gfs2_dir_get_hash_table(dip);
  985. if (IS_ERR(hc))
  986. return PTR_ERR(hc);
  987. hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
  988. if (hc2 == NULL)
  989. hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
  990. if (!hc2)
  991. return -ENOMEM;
  992. h = hc2;
  993. error = gfs2_meta_inode_buffer(dip, &dibh);
  994. if (error)
  995. goto out_kfree;
  996. for (x = 0; x < hsize; x++) {
  997. *h++ = *hc;
  998. *h++ = *hc;
  999. hc++;
  1000. }
  1001. error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
  1002. if (error != (hsize_bytes * 2))
  1003. goto fail;
  1004. gfs2_dir_hash_inval(dip);
  1005. dip->i_hash_cache = hc2;
  1006. dip->i_depth++;
  1007. gfs2_dinode_out(dip, dibh->b_data);
  1008. brelse(dibh);
  1009. return 0;
  1010. fail:
  1011. /* Replace original hash table & size */
  1012. gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
  1013. i_size_write(&dip->i_inode, hsize_bytes);
  1014. gfs2_dinode_out(dip, dibh->b_data);
  1015. brelse(dibh);
  1016. out_kfree:
  1017. kvfree(hc2);
  1018. return error;
  1019. }
  1020. /**
  1021. * compare_dents - compare directory entries by hash value
  1022. * @a: first dent
  1023. * @b: second dent
  1024. *
  1025. * When comparing the hash entries of @a to @b:
  1026. * gt: returns 1
  1027. * lt: returns -1
  1028. * eq: returns 0
  1029. */
  1030. static int compare_dents(const void *a, const void *b)
  1031. {
  1032. const struct gfs2_dirent *dent_a, *dent_b;
  1033. u32 hash_a, hash_b;
  1034. int ret = 0;
  1035. dent_a = *(const struct gfs2_dirent **)a;
  1036. hash_a = dent_a->de_cookie;
  1037. dent_b = *(const struct gfs2_dirent **)b;
  1038. hash_b = dent_b->de_cookie;
  1039. if (hash_a > hash_b)
  1040. ret = 1;
  1041. else if (hash_a < hash_b)
  1042. ret = -1;
  1043. else {
  1044. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  1045. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  1046. if (len_a > len_b)
  1047. ret = 1;
  1048. else if (len_a < len_b)
  1049. ret = -1;
  1050. else
  1051. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  1052. }
  1053. return ret;
  1054. }
  1055. /**
  1056. * do_filldir_main - read out directory entries
  1057. * @dip: The GFS2 inode
  1058. * @ctx: what to feed the entries to
  1059. * @darr: an array of struct gfs2_dirent pointers to read
  1060. * @entries: the number of entries in darr
  1061. * @copied: pointer to int that's non-zero if a entry has been copied out
  1062. *
  1063. * Jump through some hoops to make sure that if there are hash collsions,
  1064. * they are read out at the beginning of a buffer. We want to minimize
  1065. * the possibility that they will fall into different readdir buffers or
  1066. * that someone will want to seek to that location.
  1067. *
  1068. * Returns: errno, >0 if the actor tells you to stop
  1069. */
  1070. static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
  1071. struct gfs2_dirent **darr, u32 entries,
  1072. u32 sort_start, int *copied)
  1073. {
  1074. const struct gfs2_dirent *dent, *dent_next;
  1075. u64 off, off_next;
  1076. unsigned int x, y;
  1077. int run = 0;
  1078. if (sort_start < entries)
  1079. sort(&darr[sort_start], entries - sort_start,
  1080. sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1081. dent_next = darr[0];
  1082. off_next = dent_next->de_cookie;
  1083. for (x = 0, y = 1; x < entries; x++, y++) {
  1084. dent = dent_next;
  1085. off = off_next;
  1086. if (y < entries) {
  1087. dent_next = darr[y];
  1088. off_next = dent_next->de_cookie;
  1089. if (off < ctx->pos)
  1090. continue;
  1091. ctx->pos = off;
  1092. if (off_next == off) {
  1093. if (*copied && !run)
  1094. return 1;
  1095. run = 1;
  1096. } else
  1097. run = 0;
  1098. } else {
  1099. if (off < ctx->pos)
  1100. continue;
  1101. ctx->pos = off;
  1102. }
  1103. if (!dir_emit(ctx, (const char *)(dent + 1),
  1104. be16_to_cpu(dent->de_name_len),
  1105. be64_to_cpu(dent->de_inum.no_addr),
  1106. be16_to_cpu(dent->de_type)))
  1107. return 1;
  1108. *copied = 1;
  1109. }
  1110. /* Increment the ctx->pos by one, so the next time we come into the
  1111. do_filldir fxn, we get the next entry instead of the last one in the
  1112. current leaf */
  1113. ctx->pos++;
  1114. return 0;
  1115. }
  1116. static void *gfs2_alloc_sort_buffer(unsigned size)
  1117. {
  1118. void *ptr = NULL;
  1119. if (size < KMALLOC_MAX_SIZE)
  1120. ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
  1121. if (!ptr)
  1122. ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
  1123. return ptr;
  1124. }
  1125. static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
  1126. unsigned leaf_nr, struct gfs2_dirent **darr,
  1127. unsigned entries)
  1128. {
  1129. int sort_id = -1;
  1130. int i;
  1131. for (i = 0; i < entries; i++) {
  1132. unsigned offset;
  1133. darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash);
  1134. darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie);
  1135. if (!sdp->sd_args.ar_loccookie)
  1136. continue;
  1137. offset = (char *)(darr[i]) -
  1138. (bh->b_data + gfs2_dirent_offset(bh->b_data));
  1139. offset /= GFS2_MIN_DIRENT_SIZE;
  1140. offset += leaf_nr * sdp->sd_max_dents_per_leaf;
  1141. if (offset >= GFS2_USE_HASH_FLAG ||
  1142. leaf_nr >= GFS2_USE_HASH_FLAG) {
  1143. darr[i]->de_cookie |= GFS2_USE_HASH_FLAG;
  1144. if (sort_id < 0)
  1145. sort_id = i;
  1146. continue;
  1147. }
  1148. darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK;
  1149. darr[i]->de_cookie |= offset;
  1150. }
  1151. return sort_id;
  1152. }
  1153. static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
  1154. int *copied, unsigned *depth,
  1155. u64 leaf_no)
  1156. {
  1157. struct gfs2_inode *ip = GFS2_I(inode);
  1158. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1159. struct buffer_head *bh;
  1160. struct gfs2_leaf *lf;
  1161. unsigned entries = 0, entries2 = 0;
  1162. unsigned leaves = 0, leaf = 0, offset, sort_offset;
  1163. struct gfs2_dirent **darr, *dent;
  1164. struct dirent_gather g;
  1165. struct buffer_head **larr;
  1166. int error, i, need_sort = 0, sort_id;
  1167. u64 lfn = leaf_no;
  1168. do {
  1169. error = get_leaf(ip, lfn, &bh);
  1170. if (error)
  1171. goto out;
  1172. lf = (struct gfs2_leaf *)bh->b_data;
  1173. if (leaves == 0)
  1174. *depth = be16_to_cpu(lf->lf_depth);
  1175. entries += be16_to_cpu(lf->lf_entries);
  1176. leaves++;
  1177. lfn = be64_to_cpu(lf->lf_next);
  1178. brelse(bh);
  1179. } while(lfn);
  1180. if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) {
  1181. need_sort = 1;
  1182. sort_offset = 0;
  1183. }
  1184. if (!entries)
  1185. return 0;
  1186. error = -ENOMEM;
  1187. /*
  1188. * The extra 99 entries are not normally used, but are a buffer
  1189. * zone in case the number of entries in the leaf is corrupt.
  1190. * 99 is the maximum number of entries that can fit in a single
  1191. * leaf block.
  1192. */
  1193. larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
  1194. if (!larr)
  1195. goto out;
  1196. darr = (struct gfs2_dirent **)(larr + leaves);
  1197. g.pdent = (const struct gfs2_dirent **)darr;
  1198. g.offset = 0;
  1199. lfn = leaf_no;
  1200. do {
  1201. error = get_leaf(ip, lfn, &bh);
  1202. if (error)
  1203. goto out_free;
  1204. lf = (struct gfs2_leaf *)bh->b_data;
  1205. lfn = be64_to_cpu(lf->lf_next);
  1206. if (lf->lf_entries) {
  1207. offset = g.offset;
  1208. entries2 += be16_to_cpu(lf->lf_entries);
  1209. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1210. gfs2_dirent_gather, NULL, &g);
  1211. error = PTR_ERR(dent);
  1212. if (IS_ERR(dent))
  1213. goto out_free;
  1214. if (entries2 != g.offset) {
  1215. fs_warn(sdp, "Number of entries corrupt in dir "
  1216. "leaf %llu, entries2 (%u) != "
  1217. "g.offset (%u)\n",
  1218. (unsigned long long)bh->b_blocknr,
  1219. entries2, g.offset);
  1220. error = -EIO;
  1221. goto out_free;
  1222. }
  1223. error = 0;
  1224. sort_id = gfs2_set_cookies(sdp, bh, leaf, &darr[offset],
  1225. be16_to_cpu(lf->lf_entries));
  1226. if (!need_sort && sort_id >= 0) {
  1227. need_sort = 1;
  1228. sort_offset = offset + sort_id;
  1229. }
  1230. larr[leaf++] = bh;
  1231. } else {
  1232. larr[leaf++] = NULL;
  1233. brelse(bh);
  1234. }
  1235. } while(lfn);
  1236. BUG_ON(entries2 != entries);
  1237. error = do_filldir_main(ip, ctx, darr, entries, need_sort ?
  1238. sort_offset : entries, copied);
  1239. out_free:
  1240. for(i = 0; i < leaf; i++)
  1241. if (larr[i])
  1242. brelse(larr[i]);
  1243. kvfree(larr);
  1244. out:
  1245. return error;
  1246. }
  1247. /**
  1248. * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
  1249. *
  1250. * Note: we can't calculate each index like dir_e_read can because we don't
  1251. * have the leaf, and therefore we don't have the depth, and therefore we
  1252. * don't have the length. So we have to just read enough ahead to make up
  1253. * for the loss of information.
  1254. */
  1255. static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
  1256. struct file_ra_state *f_ra)
  1257. {
  1258. struct gfs2_inode *ip = GFS2_I(inode);
  1259. struct gfs2_glock *gl = ip->i_gl;
  1260. struct buffer_head *bh;
  1261. u64 blocknr = 0, last;
  1262. unsigned count;
  1263. /* First check if we've already read-ahead for the whole range. */
  1264. if (index + MAX_RA_BLOCKS < f_ra->start)
  1265. return;
  1266. f_ra->start = max((pgoff_t)index, f_ra->start);
  1267. for (count = 0; count < MAX_RA_BLOCKS; count++) {
  1268. if (f_ra->start >= hsize) /* if exceeded the hash table */
  1269. break;
  1270. last = blocknr;
  1271. blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
  1272. f_ra->start++;
  1273. if (blocknr == last)
  1274. continue;
  1275. bh = gfs2_getbuf(gl, blocknr, 1);
  1276. if (trylock_buffer(bh)) {
  1277. if (buffer_uptodate(bh)) {
  1278. unlock_buffer(bh);
  1279. brelse(bh);
  1280. continue;
  1281. }
  1282. bh->b_end_io = end_buffer_read_sync;
  1283. submit_bh(READA | REQ_META, bh);
  1284. continue;
  1285. }
  1286. brelse(bh);
  1287. }
  1288. }
  1289. /**
  1290. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1291. * @dip: dinode pointer
  1292. * @ctx: actor to feed the entries to
  1293. *
  1294. * Returns: errno
  1295. */
  1296. static int dir_e_read(struct inode *inode, struct dir_context *ctx,
  1297. struct file_ra_state *f_ra)
  1298. {
  1299. struct gfs2_inode *dip = GFS2_I(inode);
  1300. u32 hsize, len = 0;
  1301. u32 hash, index;
  1302. __be64 *lp;
  1303. int copied = 0;
  1304. int error = 0;
  1305. unsigned depth = 0;
  1306. hsize = 1 << dip->i_depth;
  1307. hash = gfs2_dir_offset2hash(ctx->pos);
  1308. index = hash >> (32 - dip->i_depth);
  1309. if (dip->i_hash_cache == NULL)
  1310. f_ra->start = 0;
  1311. lp = gfs2_dir_get_hash_table(dip);
  1312. if (IS_ERR(lp))
  1313. return PTR_ERR(lp);
  1314. gfs2_dir_readahead(inode, hsize, index, f_ra);
  1315. while (index < hsize) {
  1316. error = gfs2_dir_read_leaf(inode, ctx,
  1317. &copied, &depth,
  1318. be64_to_cpu(lp[index]));
  1319. if (error)
  1320. break;
  1321. len = 1 << (dip->i_depth - depth);
  1322. index = (index & ~(len - 1)) + len;
  1323. }
  1324. if (error > 0)
  1325. error = 0;
  1326. return error;
  1327. }
  1328. int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
  1329. struct file_ra_state *f_ra)
  1330. {
  1331. struct gfs2_inode *dip = GFS2_I(inode);
  1332. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1333. struct dirent_gather g;
  1334. struct gfs2_dirent **darr, *dent;
  1335. struct buffer_head *dibh;
  1336. int copied = 0;
  1337. int error;
  1338. if (!dip->i_entries)
  1339. return 0;
  1340. if (dip->i_diskflags & GFS2_DIF_EXHASH)
  1341. return dir_e_read(inode, ctx, f_ra);
  1342. if (!gfs2_is_stuffed(dip)) {
  1343. gfs2_consist_inode(dip);
  1344. return -EIO;
  1345. }
  1346. error = gfs2_meta_inode_buffer(dip, &dibh);
  1347. if (error)
  1348. return error;
  1349. error = -ENOMEM;
  1350. /* 96 is max number of dirents which can be stuffed into an inode */
  1351. darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
  1352. if (darr) {
  1353. g.pdent = (const struct gfs2_dirent **)darr;
  1354. g.offset = 0;
  1355. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1356. gfs2_dirent_gather, NULL, &g);
  1357. if (IS_ERR(dent)) {
  1358. error = PTR_ERR(dent);
  1359. goto out;
  1360. }
  1361. if (dip->i_entries != g.offset) {
  1362. fs_warn(sdp, "Number of entries corrupt in dir %llu, "
  1363. "ip->i_entries (%u) != g.offset (%u)\n",
  1364. (unsigned long long)dip->i_no_addr,
  1365. dip->i_entries,
  1366. g.offset);
  1367. error = -EIO;
  1368. goto out;
  1369. }
  1370. gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries);
  1371. error = do_filldir_main(dip, ctx, darr,
  1372. dip->i_entries, 0, &copied);
  1373. out:
  1374. kfree(darr);
  1375. }
  1376. if (error > 0)
  1377. error = 0;
  1378. brelse(dibh);
  1379. return error;
  1380. }
  1381. /**
  1382. * gfs2_dir_search - Search a directory
  1383. * @dip: The GFS2 dir inode
  1384. * @name: The name we are looking up
  1385. * @fail_on_exist: Fail if the name exists rather than looking it up
  1386. *
  1387. * This routine searches a directory for a file or another directory.
  1388. * Assumes a glock is held on dip.
  1389. *
  1390. * Returns: errno
  1391. */
  1392. struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
  1393. bool fail_on_exist)
  1394. {
  1395. struct buffer_head *bh;
  1396. struct gfs2_dirent *dent;
  1397. u64 addr, formal_ino;
  1398. u16 dtype;
  1399. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1400. if (dent) {
  1401. struct inode *inode;
  1402. u16 rahead;
  1403. if (IS_ERR(dent))
  1404. return ERR_CAST(dent);
  1405. dtype = be16_to_cpu(dent->de_type);
  1406. rahead = be16_to_cpu(dent->de_rahead);
  1407. addr = be64_to_cpu(dent->de_inum.no_addr);
  1408. formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
  1409. brelse(bh);
  1410. if (fail_on_exist)
  1411. return ERR_PTR(-EEXIST);
  1412. inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0);
  1413. if (!IS_ERR(inode))
  1414. GFS2_I(inode)->i_rahead = rahead;
  1415. return inode;
  1416. }
  1417. return ERR_PTR(-ENOENT);
  1418. }
  1419. int gfs2_dir_check(struct inode *dir, const struct qstr *name,
  1420. const struct gfs2_inode *ip)
  1421. {
  1422. struct buffer_head *bh;
  1423. struct gfs2_dirent *dent;
  1424. int ret = -ENOENT;
  1425. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1426. if (dent) {
  1427. if (IS_ERR(dent))
  1428. return PTR_ERR(dent);
  1429. if (ip) {
  1430. if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
  1431. goto out;
  1432. if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
  1433. ip->i_no_formal_ino)
  1434. goto out;
  1435. if (unlikely(IF2DT(ip->i_inode.i_mode) !=
  1436. be16_to_cpu(dent->de_type))) {
  1437. gfs2_consist_inode(GFS2_I(dir));
  1438. ret = -EIO;
  1439. goto out;
  1440. }
  1441. }
  1442. ret = 0;
  1443. out:
  1444. brelse(bh);
  1445. }
  1446. return ret;
  1447. }
  1448. /**
  1449. * dir_new_leaf - Add a new leaf onto hash chain
  1450. * @inode: The directory
  1451. * @name: The name we are adding
  1452. *
  1453. * This adds a new dir leaf onto an existing leaf when there is not
  1454. * enough space to add a new dir entry. This is a last resort after
  1455. * we've expanded the hash table to max size and also split existing
  1456. * leaf blocks, so it will only occur for very large directories.
  1457. *
  1458. * The dist parameter is set to 1 for leaf blocks directly attached
  1459. * to the hash table, 2 for one layer of indirection, 3 for two layers
  1460. * etc. We are thus able to tell the difference between an old leaf
  1461. * with dist set to zero (i.e. "don't know") and a new one where we
  1462. * set this information for debug/fsck purposes.
  1463. *
  1464. * Returns: 0 on success, or -ve on error
  1465. */
  1466. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1467. {
  1468. struct buffer_head *bh, *obh;
  1469. struct gfs2_inode *ip = GFS2_I(inode);
  1470. struct gfs2_leaf *leaf, *oleaf;
  1471. u32 dist = 1;
  1472. int error;
  1473. u32 index;
  1474. u64 bn;
  1475. index = name->hash >> (32 - ip->i_depth);
  1476. error = get_first_leaf(ip, index, &obh);
  1477. if (error)
  1478. return error;
  1479. do {
  1480. dist++;
  1481. oleaf = (struct gfs2_leaf *)obh->b_data;
  1482. bn = be64_to_cpu(oleaf->lf_next);
  1483. if (!bn)
  1484. break;
  1485. brelse(obh);
  1486. error = get_leaf(ip, bn, &obh);
  1487. if (error)
  1488. return error;
  1489. } while(1);
  1490. gfs2_trans_add_meta(ip->i_gl, obh);
  1491. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1492. if (!leaf) {
  1493. brelse(obh);
  1494. return -ENOSPC;
  1495. }
  1496. leaf->lf_dist = cpu_to_be32(dist);
  1497. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1498. brelse(bh);
  1499. brelse(obh);
  1500. error = gfs2_meta_inode_buffer(ip, &bh);
  1501. if (error)
  1502. return error;
  1503. gfs2_trans_add_meta(ip->i_gl, bh);
  1504. gfs2_add_inode_blocks(&ip->i_inode, 1);
  1505. gfs2_dinode_out(ip, bh->b_data);
  1506. brelse(bh);
  1507. return 0;
  1508. }
  1509. static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
  1510. {
  1511. u64 where = ip->i_no_addr + 1;
  1512. if (ip->i_eattr == where)
  1513. return 1;
  1514. return 0;
  1515. }
  1516. /**
  1517. * gfs2_dir_add - Add new filename into directory
  1518. * @inode: The directory inode
  1519. * @name: The new name
  1520. * @nip: The GFS2 inode to be linked in to the directory
  1521. * @da: The directory addition info
  1522. *
  1523. * If the call to gfs2_diradd_alloc_required resulted in there being
  1524. * no need to allocate any new directory blocks, then it will contain
  1525. * a pointer to the directory entry and the bh in which it resides. We
  1526. * can use that without having to repeat the search. If there was no
  1527. * free space, then we must now create more space.
  1528. *
  1529. * Returns: 0 on success, error code on failure
  1530. */
  1531. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1532. const struct gfs2_inode *nip, struct gfs2_diradd *da)
  1533. {
  1534. struct gfs2_inode *ip = GFS2_I(inode);
  1535. struct buffer_head *bh = da->bh;
  1536. struct gfs2_dirent *dent = da->dent;
  1537. struct timespec tv;
  1538. struct gfs2_leaf *leaf;
  1539. int error;
  1540. while(1) {
  1541. if (da->bh == NULL) {
  1542. dent = gfs2_dirent_search(inode, name,
  1543. gfs2_dirent_find_space, &bh);
  1544. }
  1545. if (dent) {
  1546. if (IS_ERR(dent))
  1547. return PTR_ERR(dent);
  1548. dent = gfs2_init_dirent(inode, dent, name, bh);
  1549. gfs2_inum_out(nip, dent);
  1550. dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
  1551. dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
  1552. tv = CURRENT_TIME;
  1553. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  1554. leaf = (struct gfs2_leaf *)bh->b_data;
  1555. be16_add_cpu(&leaf->lf_entries, 1);
  1556. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  1557. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  1558. }
  1559. da->dent = NULL;
  1560. da->bh = NULL;
  1561. brelse(bh);
  1562. ip->i_entries++;
  1563. ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
  1564. if (S_ISDIR(nip->i_inode.i_mode))
  1565. inc_nlink(&ip->i_inode);
  1566. mark_inode_dirty(inode);
  1567. error = 0;
  1568. break;
  1569. }
  1570. if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1571. error = dir_make_exhash(inode);
  1572. if (error)
  1573. break;
  1574. continue;
  1575. }
  1576. error = dir_split_leaf(inode, name);
  1577. if (error == 0)
  1578. continue;
  1579. if (error < 0)
  1580. break;
  1581. if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
  1582. error = dir_double_exhash(ip);
  1583. if (error)
  1584. break;
  1585. error = dir_split_leaf(inode, name);
  1586. if (error < 0)
  1587. break;
  1588. if (error == 0)
  1589. continue;
  1590. }
  1591. error = dir_new_leaf(inode, name);
  1592. if (!error)
  1593. continue;
  1594. error = -ENOSPC;
  1595. break;
  1596. }
  1597. return error;
  1598. }
  1599. /**
  1600. * gfs2_dir_del - Delete a directory entry
  1601. * @dip: The GFS2 inode
  1602. * @filename: The filename
  1603. *
  1604. * Returns: 0 on success, error code on failure
  1605. */
  1606. int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
  1607. {
  1608. const struct qstr *name = &dentry->d_name;
  1609. struct gfs2_dirent *dent, *prev = NULL;
  1610. struct buffer_head *bh;
  1611. struct timespec tv = CURRENT_TIME;
  1612. /* Returns _either_ the entry (if its first in block) or the
  1613. previous entry otherwise */
  1614. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1615. if (!dent) {
  1616. gfs2_consist_inode(dip);
  1617. return -EIO;
  1618. }
  1619. if (IS_ERR(dent)) {
  1620. gfs2_consist_inode(dip);
  1621. return PTR_ERR(dent);
  1622. }
  1623. /* If not first in block, adjust pointers accordingly */
  1624. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1625. prev = dent;
  1626. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1627. }
  1628. dirent_del(dip, bh, prev, dent);
  1629. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1630. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1631. u16 entries = be16_to_cpu(leaf->lf_entries);
  1632. if (!entries)
  1633. gfs2_consist_inode(dip);
  1634. leaf->lf_entries = cpu_to_be16(--entries);
  1635. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  1636. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  1637. }
  1638. brelse(bh);
  1639. if (!dip->i_entries)
  1640. gfs2_consist_inode(dip);
  1641. dip->i_entries--;
  1642. dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
  1643. if (d_is_dir(dentry))
  1644. drop_nlink(&dip->i_inode);
  1645. mark_inode_dirty(&dip->i_inode);
  1646. return 0;
  1647. }
  1648. /**
  1649. * gfs2_dir_mvino - Change inode number of directory entry
  1650. * @dip: The GFS2 inode
  1651. * @filename:
  1652. * @new_inode:
  1653. *
  1654. * This routine changes the inode number of a directory entry. It's used
  1655. * by rename to change ".." when a directory is moved.
  1656. * Assumes a glock is held on dvp.
  1657. *
  1658. * Returns: errno
  1659. */
  1660. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1661. const struct gfs2_inode *nip, unsigned int new_type)
  1662. {
  1663. struct buffer_head *bh;
  1664. struct gfs2_dirent *dent;
  1665. int error;
  1666. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1667. if (!dent) {
  1668. gfs2_consist_inode(dip);
  1669. return -EIO;
  1670. }
  1671. if (IS_ERR(dent))
  1672. return PTR_ERR(dent);
  1673. gfs2_trans_add_meta(dip->i_gl, bh);
  1674. gfs2_inum_out(nip, dent);
  1675. dent->de_type = cpu_to_be16(new_type);
  1676. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1677. brelse(bh);
  1678. error = gfs2_meta_inode_buffer(dip, &bh);
  1679. if (error)
  1680. return error;
  1681. gfs2_trans_add_meta(dip->i_gl, bh);
  1682. }
  1683. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1684. gfs2_dinode_out(dip, bh->b_data);
  1685. brelse(bh);
  1686. return 0;
  1687. }
  1688. /**
  1689. * leaf_dealloc - Deallocate a directory leaf
  1690. * @dip: the directory
  1691. * @index: the hash table offset in the directory
  1692. * @len: the number of pointers to this leaf
  1693. * @leaf_no: the leaf number
  1694. * @leaf_bh: buffer_head for the starting leaf
  1695. * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
  1696. *
  1697. * Returns: errno
  1698. */
  1699. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1700. u64 leaf_no, struct buffer_head *leaf_bh,
  1701. int last_dealloc)
  1702. {
  1703. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1704. struct gfs2_leaf *tmp_leaf;
  1705. struct gfs2_rgrp_list rlist;
  1706. struct buffer_head *bh, *dibh;
  1707. u64 blk, nblk;
  1708. unsigned int rg_blocks = 0, l_blocks = 0;
  1709. char *ht;
  1710. unsigned int x, size = len * sizeof(u64);
  1711. int error;
  1712. error = gfs2_rindex_update(sdp);
  1713. if (error)
  1714. return error;
  1715. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1716. ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
  1717. if (ht == NULL)
  1718. ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO,
  1719. PAGE_KERNEL);
  1720. if (!ht)
  1721. return -ENOMEM;
  1722. error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1723. if (error)
  1724. goto out;
  1725. /* Count the number of leaves */
  1726. bh = leaf_bh;
  1727. for (blk = leaf_no; blk; blk = nblk) {
  1728. if (blk != leaf_no) {
  1729. error = get_leaf(dip, blk, &bh);
  1730. if (error)
  1731. goto out_rlist;
  1732. }
  1733. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1734. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1735. if (blk != leaf_no)
  1736. brelse(bh);
  1737. gfs2_rlist_add(dip, &rlist, blk);
  1738. l_blocks++;
  1739. }
  1740. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1741. for (x = 0; x < rlist.rl_rgrps; x++) {
  1742. struct gfs2_rgrpd *rgd;
  1743. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1744. rg_blocks += rgd->rd_length;
  1745. }
  1746. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1747. if (error)
  1748. goto out_rlist;
  1749. error = gfs2_trans_begin(sdp,
  1750. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1751. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1752. if (error)
  1753. goto out_rg_gunlock;
  1754. bh = leaf_bh;
  1755. for (blk = leaf_no; blk; blk = nblk) {
  1756. if (blk != leaf_no) {
  1757. error = get_leaf(dip, blk, &bh);
  1758. if (error)
  1759. goto out_end_trans;
  1760. }
  1761. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1762. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1763. if (blk != leaf_no)
  1764. brelse(bh);
  1765. gfs2_free_meta(dip, blk, 1);
  1766. gfs2_add_inode_blocks(&dip->i_inode, -1);
  1767. }
  1768. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1769. if (error != size) {
  1770. if (error >= 0)
  1771. error = -EIO;
  1772. goto out_end_trans;
  1773. }
  1774. error = gfs2_meta_inode_buffer(dip, &dibh);
  1775. if (error)
  1776. goto out_end_trans;
  1777. gfs2_trans_add_meta(dip->i_gl, dibh);
  1778. /* On the last dealloc, make this a regular file in case we crash.
  1779. (We don't want to free these blocks a second time.) */
  1780. if (last_dealloc)
  1781. dip->i_inode.i_mode = S_IFREG;
  1782. gfs2_dinode_out(dip, dibh->b_data);
  1783. brelse(dibh);
  1784. out_end_trans:
  1785. gfs2_trans_end(sdp);
  1786. out_rg_gunlock:
  1787. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1788. out_rlist:
  1789. gfs2_rlist_free(&rlist);
  1790. gfs2_quota_unhold(dip);
  1791. out:
  1792. kvfree(ht);
  1793. return error;
  1794. }
  1795. /**
  1796. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1797. * @dip: the directory
  1798. *
  1799. * Dealloc all on-disk directory leaves to FREEMETA state
  1800. * Change on-disk inode type to "regular file"
  1801. *
  1802. * Returns: errno
  1803. */
  1804. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1805. {
  1806. struct buffer_head *bh;
  1807. struct gfs2_leaf *leaf;
  1808. u32 hsize, len;
  1809. u32 index = 0, next_index;
  1810. __be64 *lp;
  1811. u64 leaf_no;
  1812. int error = 0, last;
  1813. hsize = 1 << dip->i_depth;
  1814. lp = gfs2_dir_get_hash_table(dip);
  1815. if (IS_ERR(lp))
  1816. return PTR_ERR(lp);
  1817. while (index < hsize) {
  1818. leaf_no = be64_to_cpu(lp[index]);
  1819. if (leaf_no) {
  1820. error = get_leaf(dip, leaf_no, &bh);
  1821. if (error)
  1822. goto out;
  1823. leaf = (struct gfs2_leaf *)bh->b_data;
  1824. len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
  1825. next_index = (index & ~(len - 1)) + len;
  1826. last = ((next_index >= hsize) ? 1 : 0);
  1827. error = leaf_dealloc(dip, index, len, leaf_no, bh,
  1828. last);
  1829. brelse(bh);
  1830. if (error)
  1831. goto out;
  1832. index = next_index;
  1833. } else
  1834. index++;
  1835. }
  1836. if (index != hsize) {
  1837. gfs2_consist_inode(dip);
  1838. error = -EIO;
  1839. }
  1840. out:
  1841. return error;
  1842. }
  1843. /**
  1844. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1845. * @ip: the file being written to
  1846. * @filname: the filename that's going to be added
  1847. * @da: The structure to return dir alloc info
  1848. *
  1849. * Returns: 0 if ok, -ve on error
  1850. */
  1851. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
  1852. struct gfs2_diradd *da)
  1853. {
  1854. struct gfs2_inode *ip = GFS2_I(inode);
  1855. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1856. const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
  1857. struct gfs2_dirent *dent;
  1858. struct buffer_head *bh;
  1859. da->nr_blocks = 0;
  1860. da->bh = NULL;
  1861. da->dent = NULL;
  1862. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1863. if (!dent) {
  1864. da->nr_blocks = sdp->sd_max_dirres;
  1865. if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
  1866. (GFS2_DIRENT_SIZE(name->len) < extra))
  1867. da->nr_blocks = 1;
  1868. return 0;
  1869. }
  1870. if (IS_ERR(dent))
  1871. return PTR_ERR(dent);
  1872. if (da->save_loc) {
  1873. da->bh = bh;
  1874. da->dent = dent;
  1875. } else {
  1876. brelse(bh);
  1877. }
  1878. return 0;
  1879. }