dir.c 52 KB

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