dir.c 52 KB

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