dir.c 52 KB

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