dir.c 52 KB

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