dir.c 52 KB

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