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