dir.c 52 KB

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