namei.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  1. /*
  2. * linux/fs/ext4/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/jbd2.h>
  29. #include <linux/time.h>
  30. #include <linux/ext4_fs.h>
  31. #include <linux/ext4_jbd2.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/stat.h>
  34. #include <linux/string.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/bio.h>
  38. #include "namei.h"
  39. #include "xattr.h"
  40. #include "acl.h"
  41. /*
  42. * define how far ahead to read directories while searching them.
  43. */
  44. #define NAMEI_RA_CHUNKS 2
  45. #define NAMEI_RA_BLOCKS 4
  46. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  47. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  48. static struct buffer_head *ext4_append(handle_t *handle,
  49. struct inode *inode,
  50. ext4_lblk_t *block, int *err)
  51. {
  52. struct buffer_head *bh;
  53. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  54. if ((bh = ext4_bread(handle, inode, *block, 1, err))) {
  55. inode->i_size += inode->i_sb->s_blocksize;
  56. EXT4_I(inode)->i_disksize = inode->i_size;
  57. ext4_journal_get_write_access(handle,bh);
  58. }
  59. return bh;
  60. }
  61. #ifndef assert
  62. #define assert(test) J_ASSERT(test)
  63. #endif
  64. #ifndef swap
  65. #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
  66. #endif
  67. #ifdef DX_DEBUG
  68. #define dxtrace(command) command
  69. #else
  70. #define dxtrace(command)
  71. #endif
  72. struct fake_dirent
  73. {
  74. __le32 inode;
  75. __le16 rec_len;
  76. u8 name_len;
  77. u8 file_type;
  78. };
  79. struct dx_countlimit
  80. {
  81. __le16 limit;
  82. __le16 count;
  83. };
  84. struct dx_entry
  85. {
  86. __le32 hash;
  87. __le32 block;
  88. };
  89. /*
  90. * dx_root_info is laid out so that if it should somehow get overlaid by a
  91. * dirent the two low bits of the hash version will be zero. Therefore, the
  92. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  93. */
  94. struct dx_root
  95. {
  96. struct fake_dirent dot;
  97. char dot_name[4];
  98. struct fake_dirent dotdot;
  99. char dotdot_name[4];
  100. struct dx_root_info
  101. {
  102. __le32 reserved_zero;
  103. u8 hash_version;
  104. u8 info_length; /* 8 */
  105. u8 indirect_levels;
  106. u8 unused_flags;
  107. }
  108. info;
  109. struct dx_entry entries[0];
  110. };
  111. struct dx_node
  112. {
  113. struct fake_dirent fake;
  114. struct dx_entry entries[0];
  115. };
  116. struct dx_frame
  117. {
  118. struct buffer_head *bh;
  119. struct dx_entry *entries;
  120. struct dx_entry *at;
  121. };
  122. struct dx_map_entry
  123. {
  124. u32 hash;
  125. u16 offs;
  126. u16 size;
  127. };
  128. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  129. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  130. static inline unsigned dx_get_hash (struct dx_entry *entry);
  131. static void dx_set_hash (struct dx_entry *entry, unsigned value);
  132. static unsigned dx_get_count (struct dx_entry *entries);
  133. static unsigned dx_get_limit (struct dx_entry *entries);
  134. static void dx_set_count (struct dx_entry *entries, unsigned value);
  135. static void dx_set_limit (struct dx_entry *entries, unsigned value);
  136. static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
  137. static unsigned dx_node_limit (struct inode *dir);
  138. static struct dx_frame *dx_probe(struct dentry *dentry,
  139. struct inode *dir,
  140. struct dx_hash_info *hinfo,
  141. struct dx_frame *frame,
  142. int *err);
  143. static void dx_release (struct dx_frame *frames);
  144. static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
  145. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  146. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  147. static struct ext4_dir_entry_2 *dx_move_dirents (char *from, char *to,
  148. struct dx_map_entry *offsets, int count);
  149. static struct ext4_dir_entry_2* dx_pack_dirents (char *base, int size);
  150. static void dx_insert_block(struct dx_frame *frame,
  151. u32 hash, ext4_lblk_t block);
  152. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  153. struct dx_frame *frame,
  154. struct dx_frame *frames,
  155. __u32 *start_hash);
  156. static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
  157. struct ext4_dir_entry_2 **res_dir, int *err);
  158. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  159. struct inode *inode);
  160. /*
  161. * Future: use high four bits of block for coalesce-on-delete flags
  162. * Mask them off for now.
  163. */
  164. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  165. {
  166. return le32_to_cpu(entry->block) & 0x00ffffff;
  167. }
  168. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  169. {
  170. entry->block = cpu_to_le32(value);
  171. }
  172. static inline unsigned dx_get_hash (struct dx_entry *entry)
  173. {
  174. return le32_to_cpu(entry->hash);
  175. }
  176. static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
  177. {
  178. entry->hash = cpu_to_le32(value);
  179. }
  180. static inline unsigned dx_get_count (struct dx_entry *entries)
  181. {
  182. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  183. }
  184. static inline unsigned dx_get_limit (struct dx_entry *entries)
  185. {
  186. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  187. }
  188. static inline void dx_set_count (struct dx_entry *entries, unsigned value)
  189. {
  190. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  191. }
  192. static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
  193. {
  194. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  195. }
  196. static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
  197. {
  198. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  199. EXT4_DIR_REC_LEN(2) - infosize;
  200. return 0? 20: entry_space / sizeof(struct dx_entry);
  201. }
  202. static inline unsigned dx_node_limit (struct inode *dir)
  203. {
  204. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  205. return 0? 22: entry_space / sizeof(struct dx_entry);
  206. }
  207. /*
  208. * Debug
  209. */
  210. #ifdef DX_DEBUG
  211. static void dx_show_index (char * label, struct dx_entry *entries)
  212. {
  213. int i, n = dx_get_count (entries);
  214. printk("%s index ", label);
  215. for (i = 0; i < n; i++) {
  216. printk("%x->%lu ", i? dx_get_hash(entries + i) :
  217. 0, (unsigned long)dx_get_block(entries + i));
  218. }
  219. printk("\n");
  220. }
  221. struct stats
  222. {
  223. unsigned names;
  224. unsigned space;
  225. unsigned bcount;
  226. };
  227. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  228. int size, int show_names)
  229. {
  230. unsigned names = 0, space = 0;
  231. char *base = (char *) de;
  232. struct dx_hash_info h = *hinfo;
  233. printk("names: ");
  234. while ((char *) de < base + size)
  235. {
  236. if (de->inode)
  237. {
  238. if (show_names)
  239. {
  240. int len = de->name_len;
  241. char *name = de->name;
  242. while (len--) printk("%c", *name++);
  243. ext4fs_dirhash(de->name, de->name_len, &h);
  244. printk(":%x.%u ", h.hash,
  245. ((char *) de - base));
  246. }
  247. space += EXT4_DIR_REC_LEN(de->name_len);
  248. names++;
  249. }
  250. de = ext4_next_entry(de);
  251. }
  252. printk("(%i)\n", names);
  253. return (struct stats) { names, space, 1 };
  254. }
  255. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  256. struct dx_entry *entries, int levels)
  257. {
  258. unsigned blocksize = dir->i_sb->s_blocksize;
  259. unsigned count = dx_get_count (entries), names = 0, space = 0, i;
  260. unsigned bcount = 0;
  261. struct buffer_head *bh;
  262. int err;
  263. printk("%i indexed blocks...\n", count);
  264. for (i = 0; i < count; i++, entries++)
  265. {
  266. ext4_lblk_t block = dx_get_block(entries);
  267. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  268. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  269. struct stats stats;
  270. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  271. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  272. stats = levels?
  273. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  274. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  275. names += stats.names;
  276. space += stats.space;
  277. bcount += stats.bcount;
  278. brelse (bh);
  279. }
  280. if (bcount)
  281. printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
  282. names, space/bcount,(space/bcount)*100/blocksize);
  283. return (struct stats) { names, space, bcount};
  284. }
  285. #endif /* DX_DEBUG */
  286. /*
  287. * Probe for a directory leaf block to search.
  288. *
  289. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  290. * error in the directory index, and the caller should fall back to
  291. * searching the directory normally. The callers of dx_probe **MUST**
  292. * check for this error code, and make sure it never gets reflected
  293. * back to userspace.
  294. */
  295. static struct dx_frame *
  296. dx_probe(struct dentry *dentry, struct inode *dir,
  297. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  298. {
  299. unsigned count, indirect;
  300. struct dx_entry *at, *entries, *p, *q, *m;
  301. struct dx_root *root;
  302. struct buffer_head *bh;
  303. struct dx_frame *frame = frame_in;
  304. u32 hash;
  305. frame->bh = NULL;
  306. if (dentry)
  307. dir = dentry->d_parent->d_inode;
  308. if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
  309. goto fail;
  310. root = (struct dx_root *) bh->b_data;
  311. if (root->info.hash_version != DX_HASH_TEA &&
  312. root->info.hash_version != DX_HASH_HALF_MD4 &&
  313. root->info.hash_version != DX_HASH_LEGACY) {
  314. ext4_warning(dir->i_sb, __FUNCTION__,
  315. "Unrecognised inode hash code %d",
  316. root->info.hash_version);
  317. brelse(bh);
  318. *err = ERR_BAD_DX_DIR;
  319. goto fail;
  320. }
  321. hinfo->hash_version = root->info.hash_version;
  322. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  323. if (dentry)
  324. ext4fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
  325. hash = hinfo->hash;
  326. if (root->info.unused_flags & 1) {
  327. ext4_warning(dir->i_sb, __FUNCTION__,
  328. "Unimplemented inode hash flags: %#06x",
  329. root->info.unused_flags);
  330. brelse(bh);
  331. *err = ERR_BAD_DX_DIR;
  332. goto fail;
  333. }
  334. if ((indirect = root->info.indirect_levels) > 1) {
  335. ext4_warning(dir->i_sb, __FUNCTION__,
  336. "Unimplemented inode hash depth: %#06x",
  337. root->info.indirect_levels);
  338. brelse(bh);
  339. *err = ERR_BAD_DX_DIR;
  340. goto fail;
  341. }
  342. entries = (struct dx_entry *) (((char *)&root->info) +
  343. root->info.info_length);
  344. if (dx_get_limit(entries) != dx_root_limit(dir,
  345. root->info.info_length)) {
  346. ext4_warning(dir->i_sb, __FUNCTION__,
  347. "dx entry: limit != root limit");
  348. brelse(bh);
  349. *err = ERR_BAD_DX_DIR;
  350. goto fail;
  351. }
  352. dxtrace (printk("Look up %x", hash));
  353. while (1)
  354. {
  355. count = dx_get_count(entries);
  356. if (!count || count > dx_get_limit(entries)) {
  357. ext4_warning(dir->i_sb, __FUNCTION__,
  358. "dx entry: no count or count > limit");
  359. brelse(bh);
  360. *err = ERR_BAD_DX_DIR;
  361. goto fail2;
  362. }
  363. p = entries + 1;
  364. q = entries + count - 1;
  365. while (p <= q)
  366. {
  367. m = p + (q - p)/2;
  368. dxtrace(printk("."));
  369. if (dx_get_hash(m) > hash)
  370. q = m - 1;
  371. else
  372. p = m + 1;
  373. }
  374. if (0) // linear search cross check
  375. {
  376. unsigned n = count - 1;
  377. at = entries;
  378. while (n--)
  379. {
  380. dxtrace(printk(","));
  381. if (dx_get_hash(++at) > hash)
  382. {
  383. at--;
  384. break;
  385. }
  386. }
  387. assert (at == p - 1);
  388. }
  389. at = p - 1;
  390. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  391. frame->bh = bh;
  392. frame->entries = entries;
  393. frame->at = at;
  394. if (!indirect--) return frame;
  395. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  396. goto fail2;
  397. at = entries = ((struct dx_node *) bh->b_data)->entries;
  398. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  399. ext4_warning(dir->i_sb, __FUNCTION__,
  400. "dx entry: limit != node limit");
  401. brelse(bh);
  402. *err = ERR_BAD_DX_DIR;
  403. goto fail2;
  404. }
  405. frame++;
  406. frame->bh = NULL;
  407. }
  408. fail2:
  409. while (frame >= frame_in) {
  410. brelse(frame->bh);
  411. frame--;
  412. }
  413. fail:
  414. if (*err == ERR_BAD_DX_DIR)
  415. ext4_warning(dir->i_sb, __FUNCTION__,
  416. "Corrupt dir inode %ld, running e2fsck is "
  417. "recommended.", dir->i_ino);
  418. return NULL;
  419. }
  420. static void dx_release (struct dx_frame *frames)
  421. {
  422. if (frames[0].bh == NULL)
  423. return;
  424. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  425. brelse(frames[1].bh);
  426. brelse(frames[0].bh);
  427. }
  428. /*
  429. * This function increments the frame pointer to search the next leaf
  430. * block, and reads in the necessary intervening nodes if the search
  431. * should be necessary. Whether or not the search is necessary is
  432. * controlled by the hash parameter. If the hash value is even, then
  433. * the search is only continued if the next block starts with that
  434. * hash value. This is used if we are searching for a specific file.
  435. *
  436. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  437. *
  438. * This function returns 1 if the caller should continue to search,
  439. * or 0 if it should not. If there is an error reading one of the
  440. * index blocks, it will a negative error code.
  441. *
  442. * If start_hash is non-null, it will be filled in with the starting
  443. * hash of the next page.
  444. */
  445. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  446. struct dx_frame *frame,
  447. struct dx_frame *frames,
  448. __u32 *start_hash)
  449. {
  450. struct dx_frame *p;
  451. struct buffer_head *bh;
  452. int err, num_frames = 0;
  453. __u32 bhash;
  454. p = frame;
  455. /*
  456. * Find the next leaf page by incrementing the frame pointer.
  457. * If we run out of entries in the interior node, loop around and
  458. * increment pointer in the parent node. When we break out of
  459. * this loop, num_frames indicates the number of interior
  460. * nodes need to be read.
  461. */
  462. while (1) {
  463. if (++(p->at) < p->entries + dx_get_count(p->entries))
  464. break;
  465. if (p == frames)
  466. return 0;
  467. num_frames++;
  468. p--;
  469. }
  470. /*
  471. * If the hash is 1, then continue only if the next page has a
  472. * continuation hash of any value. This is used for readdir
  473. * handling. Otherwise, check to see if the hash matches the
  474. * desired contiuation hash. If it doesn't, return since
  475. * there's no point to read in the successive index pages.
  476. */
  477. bhash = dx_get_hash(p->at);
  478. if (start_hash)
  479. *start_hash = bhash;
  480. if ((hash & 1) == 0) {
  481. if ((bhash & ~1) != hash)
  482. return 0;
  483. }
  484. /*
  485. * If the hash is HASH_NB_ALWAYS, we always go to the next
  486. * block so no check is necessary
  487. */
  488. while (num_frames--) {
  489. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  490. 0, &err)))
  491. return err; /* Failure */
  492. p++;
  493. brelse (p->bh);
  494. p->bh = bh;
  495. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  496. }
  497. return 1;
  498. }
  499. /*
  500. * p is at least 6 bytes before the end of page
  501. */
  502. static inline struct ext4_dir_entry_2 *ext4_next_entry(struct ext4_dir_entry_2 *p)
  503. {
  504. return (struct ext4_dir_entry_2 *)((char *)p +
  505. ext4_rec_len_from_disk(p->rec_len));
  506. }
  507. /*
  508. * This function fills a red-black tree with information from a
  509. * directory block. It returns the number directory entries loaded
  510. * into the tree. If there is an error it is returned in err.
  511. */
  512. static int htree_dirblock_to_tree(struct file *dir_file,
  513. struct inode *dir, ext4_lblk_t block,
  514. struct dx_hash_info *hinfo,
  515. __u32 start_hash, __u32 start_minor_hash)
  516. {
  517. struct buffer_head *bh;
  518. struct ext4_dir_entry_2 *de, *top;
  519. int err, count = 0;
  520. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  521. (unsigned long)block));
  522. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  523. return err;
  524. de = (struct ext4_dir_entry_2 *) bh->b_data;
  525. top = (struct ext4_dir_entry_2 *) ((char *) de +
  526. dir->i_sb->s_blocksize -
  527. EXT4_DIR_REC_LEN(0));
  528. for (; de < top; de = ext4_next_entry(de)) {
  529. if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
  530. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  531. +((char *)de - bh->b_data))) {
  532. /* On error, skip the f_pos to the next block. */
  533. dir_file->f_pos = (dir_file->f_pos |
  534. (dir->i_sb->s_blocksize - 1)) + 1;
  535. brelse (bh);
  536. return count;
  537. }
  538. ext4fs_dirhash(de->name, de->name_len, hinfo);
  539. if ((hinfo->hash < start_hash) ||
  540. ((hinfo->hash == start_hash) &&
  541. (hinfo->minor_hash < start_minor_hash)))
  542. continue;
  543. if (de->inode == 0)
  544. continue;
  545. if ((err = ext4_htree_store_dirent(dir_file,
  546. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  547. brelse(bh);
  548. return err;
  549. }
  550. count++;
  551. }
  552. brelse(bh);
  553. return count;
  554. }
  555. /*
  556. * This function fills a red-black tree with information from a
  557. * directory. We start scanning the directory in hash order, starting
  558. * at start_hash and start_minor_hash.
  559. *
  560. * This function returns the number of entries inserted into the tree,
  561. * or a negative error code.
  562. */
  563. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  564. __u32 start_minor_hash, __u32 *next_hash)
  565. {
  566. struct dx_hash_info hinfo;
  567. struct ext4_dir_entry_2 *de;
  568. struct dx_frame frames[2], *frame;
  569. struct inode *dir;
  570. ext4_lblk_t block;
  571. int count = 0;
  572. int ret, err;
  573. __u32 hashval;
  574. dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
  575. start_minor_hash));
  576. dir = dir_file->f_path.dentry->d_inode;
  577. if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
  578. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  579. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  580. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  581. start_hash, start_minor_hash);
  582. *next_hash = ~0;
  583. return count;
  584. }
  585. hinfo.hash = start_hash;
  586. hinfo.minor_hash = 0;
  587. frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
  588. if (!frame)
  589. return err;
  590. /* Add '.' and '..' from the htree header */
  591. if (!start_hash && !start_minor_hash) {
  592. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  593. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  594. goto errout;
  595. count++;
  596. }
  597. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  598. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  599. de = ext4_next_entry(de);
  600. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  601. goto errout;
  602. count++;
  603. }
  604. while (1) {
  605. block = dx_get_block(frame->at);
  606. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  607. start_hash, start_minor_hash);
  608. if (ret < 0) {
  609. err = ret;
  610. goto errout;
  611. }
  612. count += ret;
  613. hashval = ~0;
  614. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  615. frame, frames, &hashval);
  616. *next_hash = hashval;
  617. if (ret < 0) {
  618. err = ret;
  619. goto errout;
  620. }
  621. /*
  622. * Stop if: (a) there are no more entries, or
  623. * (b) we have inserted at least one entry and the
  624. * next hash value is not a continuation
  625. */
  626. if ((ret == 0) ||
  627. (count && ((hashval & 1) == 0)))
  628. break;
  629. }
  630. dx_release(frames);
  631. dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
  632. count, *next_hash));
  633. return count;
  634. errout:
  635. dx_release(frames);
  636. return (err);
  637. }
  638. /*
  639. * Directory block splitting, compacting
  640. */
  641. /*
  642. * Create map of hash values, offsets, and sizes, stored at end of block.
  643. * Returns number of entries mapped.
  644. */
  645. static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
  646. struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
  647. {
  648. int count = 0;
  649. char *base = (char *) de;
  650. struct dx_hash_info h = *hinfo;
  651. while ((char *) de < base + size)
  652. {
  653. if (de->name_len && de->inode) {
  654. ext4fs_dirhash(de->name, de->name_len, &h);
  655. map_tail--;
  656. map_tail->hash = h.hash;
  657. map_tail->offs = (u16) ((char *) de - base);
  658. map_tail->size = le16_to_cpu(de->rec_len);
  659. count++;
  660. cond_resched();
  661. }
  662. /* XXX: do we need to check rec_len == 0 case? -Chris */
  663. de = ext4_next_entry(de);
  664. }
  665. return count;
  666. }
  667. /* Sort map by hash value */
  668. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  669. {
  670. struct dx_map_entry *p, *q, *top = map + count - 1;
  671. int more;
  672. /* Combsort until bubble sort doesn't suck */
  673. while (count > 2) {
  674. count = count*10/13;
  675. if (count - 9 < 2) /* 9, 10 -> 11 */
  676. count = 11;
  677. for (p = top, q = p - count; q >= map; p--, q--)
  678. if (p->hash < q->hash)
  679. swap(*p, *q);
  680. }
  681. /* Garden variety bubble sort */
  682. do {
  683. more = 0;
  684. q = top;
  685. while (q-- > map) {
  686. if (q[1].hash >= q[0].hash)
  687. continue;
  688. swap(*(q+1), *q);
  689. more = 1;
  690. }
  691. } while(more);
  692. }
  693. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  694. {
  695. struct dx_entry *entries = frame->entries;
  696. struct dx_entry *old = frame->at, *new = old + 1;
  697. int count = dx_get_count(entries);
  698. assert(count < dx_get_limit(entries));
  699. assert(old < entries + count);
  700. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  701. dx_set_hash(new, hash);
  702. dx_set_block(new, block);
  703. dx_set_count(entries, count + 1);
  704. }
  705. static void ext4_update_dx_flag(struct inode *inode)
  706. {
  707. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  708. EXT4_FEATURE_COMPAT_DIR_INDEX))
  709. EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
  710. }
  711. /*
  712. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  713. *
  714. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  715. * `de != NULL' is guaranteed by caller.
  716. */
  717. static inline int ext4_match (int len, const char * const name,
  718. struct ext4_dir_entry_2 * de)
  719. {
  720. if (len != de->name_len)
  721. return 0;
  722. if (!de->inode)
  723. return 0;
  724. return !memcmp(name, de->name, len);
  725. }
  726. /*
  727. * Returns 0 if not found, -1 on failure, and 1 on success
  728. */
  729. static inline int search_dirblock(struct buffer_head * bh,
  730. struct inode *dir,
  731. struct dentry *dentry,
  732. unsigned long offset,
  733. struct ext4_dir_entry_2 ** res_dir)
  734. {
  735. struct ext4_dir_entry_2 * de;
  736. char * dlimit;
  737. int de_len;
  738. const char *name = dentry->d_name.name;
  739. int namelen = dentry->d_name.len;
  740. de = (struct ext4_dir_entry_2 *) bh->b_data;
  741. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  742. while ((char *) de < dlimit) {
  743. /* this code is executed quadratically often */
  744. /* do minimal checking `by hand' */
  745. if ((char *) de + namelen <= dlimit &&
  746. ext4_match (namelen, name, de)) {
  747. /* found a match - just to be sure, do a full check */
  748. if (!ext4_check_dir_entry("ext4_find_entry",
  749. dir, de, bh, offset))
  750. return -1;
  751. *res_dir = de;
  752. return 1;
  753. }
  754. /* prevent looping on a bad block */
  755. de_len = ext4_rec_len_from_disk(de->rec_len);
  756. if (de_len <= 0)
  757. return -1;
  758. offset += de_len;
  759. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  760. }
  761. return 0;
  762. }
  763. /*
  764. * ext4_find_entry()
  765. *
  766. * finds an entry in the specified directory with the wanted name. It
  767. * returns the cache buffer in which the entry was found, and the entry
  768. * itself (as a parameter - res_dir). It does NOT read the inode of the
  769. * entry - you'll have to do that yourself if you want to.
  770. *
  771. * The returned buffer_head has ->b_count elevated. The caller is expected
  772. * to brelse() it when appropriate.
  773. */
  774. static struct buffer_head * ext4_find_entry (struct dentry *dentry,
  775. struct ext4_dir_entry_2 ** res_dir)
  776. {
  777. struct super_block * sb;
  778. struct buffer_head * bh_use[NAMEI_RA_SIZE];
  779. struct buffer_head * bh, *ret = NULL;
  780. ext4_lblk_t start, block, b;
  781. int ra_max = 0; /* Number of bh's in the readahead
  782. buffer, bh_use[] */
  783. int ra_ptr = 0; /* Current index into readahead
  784. buffer */
  785. int num = 0;
  786. ext4_lblk_t nblocks;
  787. int i, err;
  788. struct inode *dir = dentry->d_parent->d_inode;
  789. int namelen;
  790. const u8 *name;
  791. unsigned blocksize;
  792. *res_dir = NULL;
  793. sb = dir->i_sb;
  794. blocksize = sb->s_blocksize;
  795. namelen = dentry->d_name.len;
  796. name = dentry->d_name.name;
  797. if (namelen > EXT4_NAME_LEN)
  798. return NULL;
  799. if (is_dx(dir)) {
  800. bh = ext4_dx_find_entry(dentry, res_dir, &err);
  801. /*
  802. * On success, or if the error was file not found,
  803. * return. Otherwise, fall back to doing a search the
  804. * old fashioned way.
  805. */
  806. if (bh || (err != ERR_BAD_DX_DIR))
  807. return bh;
  808. dxtrace(printk("ext4_find_entry: dx failed, falling back\n"));
  809. }
  810. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  811. start = EXT4_I(dir)->i_dir_start_lookup;
  812. if (start >= nblocks)
  813. start = 0;
  814. block = start;
  815. restart:
  816. do {
  817. /*
  818. * We deal with the read-ahead logic here.
  819. */
  820. if (ra_ptr >= ra_max) {
  821. /* Refill the readahead buffer */
  822. ra_ptr = 0;
  823. b = block;
  824. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  825. /*
  826. * Terminate if we reach the end of the
  827. * directory and must wrap, or if our
  828. * search has finished at this block.
  829. */
  830. if (b >= nblocks || (num && block == start)) {
  831. bh_use[ra_max] = NULL;
  832. break;
  833. }
  834. num++;
  835. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  836. bh_use[ra_max] = bh;
  837. if (bh)
  838. ll_rw_block(READ_META, 1, &bh);
  839. }
  840. }
  841. if ((bh = bh_use[ra_ptr++]) == NULL)
  842. goto next;
  843. wait_on_buffer(bh);
  844. if (!buffer_uptodate(bh)) {
  845. /* read error, skip block & hope for the best */
  846. ext4_error(sb, __FUNCTION__, "reading directory #%lu "
  847. "offset %lu", dir->i_ino,
  848. (unsigned long)block);
  849. brelse(bh);
  850. goto next;
  851. }
  852. i = search_dirblock(bh, dir, dentry,
  853. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  854. if (i == 1) {
  855. EXT4_I(dir)->i_dir_start_lookup = block;
  856. ret = bh;
  857. goto cleanup_and_exit;
  858. } else {
  859. brelse(bh);
  860. if (i < 0)
  861. goto cleanup_and_exit;
  862. }
  863. next:
  864. if (++block >= nblocks)
  865. block = 0;
  866. } while (block != start);
  867. /*
  868. * If the directory has grown while we were searching, then
  869. * search the last part of the directory before giving up.
  870. */
  871. block = nblocks;
  872. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  873. if (block < nblocks) {
  874. start = 0;
  875. goto restart;
  876. }
  877. cleanup_and_exit:
  878. /* Clean up the read-ahead blocks */
  879. for (; ra_ptr < ra_max; ra_ptr++)
  880. brelse (bh_use[ra_ptr]);
  881. return ret;
  882. }
  883. static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
  884. struct ext4_dir_entry_2 **res_dir, int *err)
  885. {
  886. struct super_block * sb;
  887. struct dx_hash_info hinfo;
  888. u32 hash;
  889. struct dx_frame frames[2], *frame;
  890. struct ext4_dir_entry_2 *de, *top;
  891. struct buffer_head *bh;
  892. ext4_lblk_t block;
  893. int retval;
  894. int namelen = dentry->d_name.len;
  895. const u8 *name = dentry->d_name.name;
  896. struct inode *dir = dentry->d_parent->d_inode;
  897. sb = dir->i_sb;
  898. /* NFS may look up ".." - look at dx_root directory block */
  899. if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
  900. if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
  901. return NULL;
  902. } else {
  903. frame = frames;
  904. frame->bh = NULL; /* for dx_release() */
  905. frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
  906. dx_set_block(frame->at, 0); /* dx_root block is 0 */
  907. }
  908. hash = hinfo.hash;
  909. do {
  910. block = dx_get_block(frame->at);
  911. if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
  912. goto errout;
  913. de = (struct ext4_dir_entry_2 *) bh->b_data;
  914. top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
  915. EXT4_DIR_REC_LEN(0));
  916. for (; de < top; de = ext4_next_entry(de))
  917. if (ext4_match (namelen, name, de)) {
  918. if (!ext4_check_dir_entry("ext4_find_entry",
  919. dir, de, bh,
  920. (block<<EXT4_BLOCK_SIZE_BITS(sb))
  921. +((char *)de - bh->b_data))) {
  922. brelse (bh);
  923. *err = ERR_BAD_DX_DIR;
  924. goto errout;
  925. }
  926. *res_dir = de;
  927. dx_release (frames);
  928. return bh;
  929. }
  930. brelse (bh);
  931. /* Check to see if we should continue to search */
  932. retval = ext4_htree_next_block(dir, hash, frame,
  933. frames, NULL);
  934. if (retval < 0) {
  935. ext4_warning(sb, __FUNCTION__,
  936. "error reading index page in directory #%lu",
  937. dir->i_ino);
  938. *err = retval;
  939. goto errout;
  940. }
  941. } while (retval == 1);
  942. *err = -ENOENT;
  943. errout:
  944. dxtrace(printk("%s not found\n", name));
  945. dx_release (frames);
  946. return NULL;
  947. }
  948. static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
  949. {
  950. struct inode * inode;
  951. struct ext4_dir_entry_2 * de;
  952. struct buffer_head * bh;
  953. if (dentry->d_name.len > EXT4_NAME_LEN)
  954. return ERR_PTR(-ENAMETOOLONG);
  955. bh = ext4_find_entry(dentry, &de);
  956. inode = NULL;
  957. if (bh) {
  958. unsigned long ino = le32_to_cpu(de->inode);
  959. brelse (bh);
  960. if (!ext4_valid_inum(dir->i_sb, ino)) {
  961. ext4_error(dir->i_sb, "ext4_lookup",
  962. "bad inode number: %lu", ino);
  963. inode = NULL;
  964. } else
  965. inode = iget(dir->i_sb, ino);
  966. if (!inode)
  967. return ERR_PTR(-EACCES);
  968. if (is_bad_inode(inode)) {
  969. iput(inode);
  970. return ERR_PTR(-ENOENT);
  971. }
  972. }
  973. return d_splice_alias(inode, dentry);
  974. }
  975. struct dentry *ext4_get_parent(struct dentry *child)
  976. {
  977. unsigned long ino;
  978. struct dentry *parent;
  979. struct inode *inode;
  980. struct dentry dotdot;
  981. struct ext4_dir_entry_2 * de;
  982. struct buffer_head *bh;
  983. dotdot.d_name.name = "..";
  984. dotdot.d_name.len = 2;
  985. dotdot.d_parent = child; /* confusing, isn't it! */
  986. bh = ext4_find_entry(&dotdot, &de);
  987. inode = NULL;
  988. if (!bh)
  989. return ERR_PTR(-ENOENT);
  990. ino = le32_to_cpu(de->inode);
  991. brelse(bh);
  992. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  993. ext4_error(child->d_inode->i_sb, "ext4_get_parent",
  994. "bad inode number: %lu", ino);
  995. inode = NULL;
  996. } else
  997. inode = iget(child->d_inode->i_sb, ino);
  998. if (!inode)
  999. return ERR_PTR(-EACCES);
  1000. if (is_bad_inode(inode)) {
  1001. iput(inode);
  1002. return ERR_PTR(-ENOENT);
  1003. }
  1004. parent = d_alloc_anon(inode);
  1005. if (!parent) {
  1006. iput(inode);
  1007. parent = ERR_PTR(-ENOMEM);
  1008. }
  1009. return parent;
  1010. }
  1011. #define S_SHIFT 12
  1012. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  1013. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  1014. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  1015. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  1016. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  1017. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  1018. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  1019. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  1020. };
  1021. static inline void ext4_set_de_type(struct super_block *sb,
  1022. struct ext4_dir_entry_2 *de,
  1023. umode_t mode) {
  1024. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1025. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1026. }
  1027. /*
  1028. * Move count entries from end of map between two memory locations.
  1029. * Returns pointer to last entry moved.
  1030. */
  1031. static struct ext4_dir_entry_2 *
  1032. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
  1033. {
  1034. unsigned rec_len = 0;
  1035. while (count--) {
  1036. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
  1037. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1038. memcpy (to, de, rec_len);
  1039. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1040. ext4_rec_len_to_disk(rec_len);
  1041. de->inode = 0;
  1042. map++;
  1043. to += rec_len;
  1044. }
  1045. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1046. }
  1047. /*
  1048. * Compact each dir entry in the range to the minimal rec_len.
  1049. * Returns pointer to last entry in range.
  1050. */
  1051. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
  1052. {
  1053. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1054. unsigned rec_len = 0;
  1055. prev = to = de;
  1056. while ((char*)de < base + size) {
  1057. next = ext4_next_entry(de);
  1058. if (de->inode && de->name_len) {
  1059. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1060. if (de > to)
  1061. memmove(to, de, rec_len);
  1062. to->rec_len = ext4_rec_len_to_disk(rec_len);
  1063. prev = to;
  1064. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1065. }
  1066. de = next;
  1067. }
  1068. return prev;
  1069. }
  1070. /*
  1071. * Split a full leaf block to make room for a new dir entry.
  1072. * Allocate a new block, and move entries so that they are approx. equally full.
  1073. * Returns pointer to de in block into which the new entry will be inserted.
  1074. */
  1075. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1076. struct buffer_head **bh,struct dx_frame *frame,
  1077. struct dx_hash_info *hinfo, int *error)
  1078. {
  1079. unsigned blocksize = dir->i_sb->s_blocksize;
  1080. unsigned count, continued;
  1081. struct buffer_head *bh2;
  1082. ext4_lblk_t newblock;
  1083. u32 hash2;
  1084. struct dx_map_entry *map;
  1085. char *data1 = (*bh)->b_data, *data2;
  1086. unsigned split, move, size, i;
  1087. struct ext4_dir_entry_2 *de = NULL, *de2;
  1088. int err = 0;
  1089. bh2 = ext4_append (handle, dir, &newblock, &err);
  1090. if (!(bh2)) {
  1091. brelse(*bh);
  1092. *bh = NULL;
  1093. goto errout;
  1094. }
  1095. BUFFER_TRACE(*bh, "get_write_access");
  1096. err = ext4_journal_get_write_access(handle, *bh);
  1097. if (err)
  1098. goto journal_error;
  1099. BUFFER_TRACE(frame->bh, "get_write_access");
  1100. err = ext4_journal_get_write_access(handle, frame->bh);
  1101. if (err)
  1102. goto journal_error;
  1103. data2 = bh2->b_data;
  1104. /* create map in the end of data2 block */
  1105. map = (struct dx_map_entry *) (data2 + blocksize);
  1106. count = dx_make_map ((struct ext4_dir_entry_2 *) data1,
  1107. blocksize, hinfo, map);
  1108. map -= count;
  1109. dx_sort_map (map, count);
  1110. /* Split the existing block in the middle, size-wise */
  1111. size = 0;
  1112. move = 0;
  1113. for (i = count-1; i >= 0; i--) {
  1114. /* is more than half of this entry in 2nd half of the block? */
  1115. if (size + map[i].size/2 > blocksize/2)
  1116. break;
  1117. size += map[i].size;
  1118. move++;
  1119. }
  1120. /* map index at which we will split */
  1121. split = count - move;
  1122. hash2 = map[split].hash;
  1123. continued = hash2 == map[split - 1].hash;
  1124. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1125. (unsigned long)dx_get_block(frame->at),
  1126. hash2, split, count-split));
  1127. /* Fancy dance to stay within two buffers */
  1128. de2 = dx_move_dirents(data1, data2, map + split, count - split);
  1129. de = dx_pack_dirents(data1,blocksize);
  1130. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1131. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2);
  1132. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1133. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1134. /* Which block gets the new entry? */
  1135. if (hinfo->hash >= hash2)
  1136. {
  1137. swap(*bh, bh2);
  1138. de = de2;
  1139. }
  1140. dx_insert_block (frame, hash2 + continued, newblock);
  1141. err = ext4_journal_dirty_metadata (handle, bh2);
  1142. if (err)
  1143. goto journal_error;
  1144. err = ext4_journal_dirty_metadata (handle, frame->bh);
  1145. if (err)
  1146. goto journal_error;
  1147. brelse (bh2);
  1148. dxtrace(dx_show_index ("frame", frame->entries));
  1149. return de;
  1150. journal_error:
  1151. brelse(*bh);
  1152. brelse(bh2);
  1153. *bh = NULL;
  1154. ext4_std_error(dir->i_sb, err);
  1155. errout:
  1156. *error = err;
  1157. return NULL;
  1158. }
  1159. /*
  1160. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1161. * it points to a directory entry which is guaranteed to be large
  1162. * enough for new directory entry. If de is NULL, then
  1163. * add_dirent_to_buf will attempt search the directory block for
  1164. * space. It will return -ENOSPC if no space is available, and -EIO
  1165. * and -EEXIST if directory entry already exists.
  1166. *
  1167. * NOTE! bh is NOT released in the case where ENOSPC is returned. In
  1168. * all other cases bh is released.
  1169. */
  1170. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1171. struct inode *inode, struct ext4_dir_entry_2 *de,
  1172. struct buffer_head * bh)
  1173. {
  1174. struct inode *dir = dentry->d_parent->d_inode;
  1175. const char *name = dentry->d_name.name;
  1176. int namelen = dentry->d_name.len;
  1177. unsigned long offset = 0;
  1178. unsigned short reclen;
  1179. int nlen, rlen, err;
  1180. char *top;
  1181. reclen = EXT4_DIR_REC_LEN(namelen);
  1182. if (!de) {
  1183. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1184. top = bh->b_data + dir->i_sb->s_blocksize - reclen;
  1185. while ((char *) de <= top) {
  1186. if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
  1187. bh, offset)) {
  1188. brelse (bh);
  1189. return -EIO;
  1190. }
  1191. if (ext4_match (namelen, name, de)) {
  1192. brelse (bh);
  1193. return -EEXIST;
  1194. }
  1195. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1196. rlen = ext4_rec_len_from_disk(de->rec_len);
  1197. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1198. break;
  1199. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1200. offset += rlen;
  1201. }
  1202. if ((char *) de > top)
  1203. return -ENOSPC;
  1204. }
  1205. BUFFER_TRACE(bh, "get_write_access");
  1206. err = ext4_journal_get_write_access(handle, bh);
  1207. if (err) {
  1208. ext4_std_error(dir->i_sb, err);
  1209. brelse(bh);
  1210. return err;
  1211. }
  1212. /* By now the buffer is marked for journaling */
  1213. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1214. rlen = ext4_rec_len_from_disk(de->rec_len);
  1215. if (de->inode) {
  1216. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1217. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen);
  1218. de->rec_len = ext4_rec_len_to_disk(nlen);
  1219. de = de1;
  1220. }
  1221. de->file_type = EXT4_FT_UNKNOWN;
  1222. if (inode) {
  1223. de->inode = cpu_to_le32(inode->i_ino);
  1224. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1225. } else
  1226. de->inode = 0;
  1227. de->name_len = namelen;
  1228. memcpy (de->name, name, namelen);
  1229. /*
  1230. * XXX shouldn't update any times until successful
  1231. * completion of syscall, but too many callers depend
  1232. * on this.
  1233. *
  1234. * XXX similarly, too many callers depend on
  1235. * ext4_new_inode() setting the times, but error
  1236. * recovery deletes the inode, so the worst that can
  1237. * happen is that the times are slightly out of date
  1238. * and/or different from the directory change time.
  1239. */
  1240. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1241. ext4_update_dx_flag(dir);
  1242. dir->i_version++;
  1243. ext4_mark_inode_dirty(handle, dir);
  1244. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  1245. err = ext4_journal_dirty_metadata(handle, bh);
  1246. if (err)
  1247. ext4_std_error(dir->i_sb, err);
  1248. brelse(bh);
  1249. return 0;
  1250. }
  1251. /*
  1252. * This converts a one block unindexed directory to a 3 block indexed
  1253. * directory, and adds the dentry to the indexed directory.
  1254. */
  1255. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1256. struct inode *inode, struct buffer_head *bh)
  1257. {
  1258. struct inode *dir = dentry->d_parent->d_inode;
  1259. const char *name = dentry->d_name.name;
  1260. int namelen = dentry->d_name.len;
  1261. struct buffer_head *bh2;
  1262. struct dx_root *root;
  1263. struct dx_frame frames[2], *frame;
  1264. struct dx_entry *entries;
  1265. struct ext4_dir_entry_2 *de, *de2;
  1266. char *data1, *top;
  1267. unsigned len;
  1268. int retval;
  1269. unsigned blocksize;
  1270. struct dx_hash_info hinfo;
  1271. ext4_lblk_t block;
  1272. struct fake_dirent *fde;
  1273. blocksize = dir->i_sb->s_blocksize;
  1274. dxtrace(printk("Creating index\n"));
  1275. retval = ext4_journal_get_write_access(handle, bh);
  1276. if (retval) {
  1277. ext4_std_error(dir->i_sb, retval);
  1278. brelse(bh);
  1279. return retval;
  1280. }
  1281. root = (struct dx_root *) bh->b_data;
  1282. bh2 = ext4_append (handle, dir, &block, &retval);
  1283. if (!(bh2)) {
  1284. brelse(bh);
  1285. return retval;
  1286. }
  1287. EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
  1288. data1 = bh2->b_data;
  1289. /* The 0th block becomes the root, move the dirents out */
  1290. fde = &root->dotdot;
  1291. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1292. ext4_rec_len_from_disk(fde->rec_len));
  1293. len = ((char *) root) + blocksize - (char *) de;
  1294. memcpy (data1, de, len);
  1295. de = (struct ext4_dir_entry_2 *) data1;
  1296. top = data1 + len;
  1297. while ((char *)(de2 = ext4_next_entry(de)) < top)
  1298. de = de2;
  1299. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1300. /* Initialize the root; the dot dirents already exist */
  1301. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1302. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2));
  1303. memset (&root->info, 0, sizeof(root->info));
  1304. root->info.info_length = sizeof(root->info);
  1305. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1306. entries = root->entries;
  1307. dx_set_block (entries, 1);
  1308. dx_set_count (entries, 1);
  1309. dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
  1310. /* Initialize as for dx_probe */
  1311. hinfo.hash_version = root->info.hash_version;
  1312. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1313. ext4fs_dirhash(name, namelen, &hinfo);
  1314. frame = frames;
  1315. frame->entries = entries;
  1316. frame->at = entries;
  1317. frame->bh = bh;
  1318. bh = bh2;
  1319. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1320. dx_release (frames);
  1321. if (!(de))
  1322. return retval;
  1323. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1324. }
  1325. /*
  1326. * ext4_add_entry()
  1327. *
  1328. * adds a file entry to the specified directory, using the same
  1329. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1330. *
  1331. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1332. * may not sleep between calling this and putting something into
  1333. * the entry, as someone else might have used it while you slept.
  1334. */
  1335. static int ext4_add_entry (handle_t *handle, struct dentry *dentry,
  1336. struct inode *inode)
  1337. {
  1338. struct inode *dir = dentry->d_parent->d_inode;
  1339. unsigned long offset;
  1340. struct buffer_head * bh;
  1341. struct ext4_dir_entry_2 *de;
  1342. struct super_block * sb;
  1343. int retval;
  1344. int dx_fallback=0;
  1345. unsigned blocksize;
  1346. ext4_lblk_t block, blocks;
  1347. sb = dir->i_sb;
  1348. blocksize = sb->s_blocksize;
  1349. if (!dentry->d_name.len)
  1350. return -EINVAL;
  1351. if (is_dx(dir)) {
  1352. retval = ext4_dx_add_entry(handle, dentry, inode);
  1353. if (!retval || (retval != ERR_BAD_DX_DIR))
  1354. return retval;
  1355. EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
  1356. dx_fallback++;
  1357. ext4_mark_inode_dirty(handle, dir);
  1358. }
  1359. blocks = dir->i_size >> sb->s_blocksize_bits;
  1360. for (block = 0, offset = 0; block < blocks; block++) {
  1361. bh = ext4_bread(handle, dir, block, 0, &retval);
  1362. if(!bh)
  1363. return retval;
  1364. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1365. if (retval != -ENOSPC)
  1366. return retval;
  1367. if (blocks == 1 && !dx_fallback &&
  1368. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1369. return make_indexed_dir(handle, dentry, inode, bh);
  1370. brelse(bh);
  1371. }
  1372. bh = ext4_append(handle, dir, &block, &retval);
  1373. if (!bh)
  1374. return retval;
  1375. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1376. de->inode = 0;
  1377. de->rec_len = ext4_rec_len_to_disk(blocksize);
  1378. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1379. }
  1380. /*
  1381. * Returns 0 for success, or a negative error value
  1382. */
  1383. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1384. struct inode *inode)
  1385. {
  1386. struct dx_frame frames[2], *frame;
  1387. struct dx_entry *entries, *at;
  1388. struct dx_hash_info hinfo;
  1389. struct buffer_head * bh;
  1390. struct inode *dir = dentry->d_parent->d_inode;
  1391. struct super_block * sb = dir->i_sb;
  1392. struct ext4_dir_entry_2 *de;
  1393. int err;
  1394. frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
  1395. if (!frame)
  1396. return err;
  1397. entries = frame->entries;
  1398. at = frame->at;
  1399. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1400. goto cleanup;
  1401. BUFFER_TRACE(bh, "get_write_access");
  1402. err = ext4_journal_get_write_access(handle, bh);
  1403. if (err)
  1404. goto journal_error;
  1405. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1406. if (err != -ENOSPC) {
  1407. bh = NULL;
  1408. goto cleanup;
  1409. }
  1410. /* Block full, should compress but for now just split */
  1411. dxtrace(printk("using %u of %u node entries\n",
  1412. dx_get_count(entries), dx_get_limit(entries)));
  1413. /* Need to split index? */
  1414. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1415. ext4_lblk_t newblock;
  1416. unsigned icount = dx_get_count(entries);
  1417. int levels = frame - frames;
  1418. struct dx_entry *entries2;
  1419. struct dx_node *node2;
  1420. struct buffer_head *bh2;
  1421. if (levels && (dx_get_count(frames->entries) ==
  1422. dx_get_limit(frames->entries))) {
  1423. ext4_warning(sb, __FUNCTION__,
  1424. "Directory index full!");
  1425. err = -ENOSPC;
  1426. goto cleanup;
  1427. }
  1428. bh2 = ext4_append (handle, dir, &newblock, &err);
  1429. if (!(bh2))
  1430. goto cleanup;
  1431. node2 = (struct dx_node *)(bh2->b_data);
  1432. entries2 = node2->entries;
  1433. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize);
  1434. node2->fake.inode = 0;
  1435. BUFFER_TRACE(frame->bh, "get_write_access");
  1436. err = ext4_journal_get_write_access(handle, frame->bh);
  1437. if (err)
  1438. goto journal_error;
  1439. if (levels) {
  1440. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1441. unsigned hash2 = dx_get_hash(entries + icount1);
  1442. dxtrace(printk("Split index %i/%i\n", icount1, icount2));
  1443. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1444. err = ext4_journal_get_write_access(handle,
  1445. frames[0].bh);
  1446. if (err)
  1447. goto journal_error;
  1448. memcpy ((char *) entries2, (char *) (entries + icount1),
  1449. icount2 * sizeof(struct dx_entry));
  1450. dx_set_count (entries, icount1);
  1451. dx_set_count (entries2, icount2);
  1452. dx_set_limit (entries2, dx_node_limit(dir));
  1453. /* Which index block gets the new entry? */
  1454. if (at - entries >= icount1) {
  1455. frame->at = at = at - entries - icount1 + entries2;
  1456. frame->entries = entries = entries2;
  1457. swap(frame->bh, bh2);
  1458. }
  1459. dx_insert_block (frames + 0, hash2, newblock);
  1460. dxtrace(dx_show_index ("node", frames[1].entries));
  1461. dxtrace(dx_show_index ("node",
  1462. ((struct dx_node *) bh2->b_data)->entries));
  1463. err = ext4_journal_dirty_metadata(handle, bh2);
  1464. if (err)
  1465. goto journal_error;
  1466. brelse (bh2);
  1467. } else {
  1468. dxtrace(printk("Creating second level index...\n"));
  1469. memcpy((char *) entries2, (char *) entries,
  1470. icount * sizeof(struct dx_entry));
  1471. dx_set_limit(entries2, dx_node_limit(dir));
  1472. /* Set up root */
  1473. dx_set_count(entries, 1);
  1474. dx_set_block(entries + 0, newblock);
  1475. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1476. /* Add new access path frame */
  1477. frame = frames + 1;
  1478. frame->at = at = at - entries + entries2;
  1479. frame->entries = entries = entries2;
  1480. frame->bh = bh2;
  1481. err = ext4_journal_get_write_access(handle,
  1482. frame->bh);
  1483. if (err)
  1484. goto journal_error;
  1485. }
  1486. ext4_journal_dirty_metadata(handle, frames[0].bh);
  1487. }
  1488. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1489. if (!de)
  1490. goto cleanup;
  1491. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1492. bh = NULL;
  1493. goto cleanup;
  1494. journal_error:
  1495. ext4_std_error(dir->i_sb, err);
  1496. cleanup:
  1497. if (bh)
  1498. brelse(bh);
  1499. dx_release(frames);
  1500. return err;
  1501. }
  1502. /*
  1503. * ext4_delete_entry deletes a directory entry by merging it with the
  1504. * previous entry
  1505. */
  1506. static int ext4_delete_entry (handle_t *handle,
  1507. struct inode * dir,
  1508. struct ext4_dir_entry_2 * de_del,
  1509. struct buffer_head * bh)
  1510. {
  1511. struct ext4_dir_entry_2 * de, * pde;
  1512. int i;
  1513. i = 0;
  1514. pde = NULL;
  1515. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1516. while (i < bh->b_size) {
  1517. if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
  1518. return -EIO;
  1519. if (de == de_del) {
  1520. BUFFER_TRACE(bh, "get_write_access");
  1521. ext4_journal_get_write_access(handle, bh);
  1522. if (pde)
  1523. pde->rec_len = ext4_rec_len_to_disk(
  1524. ext4_rec_len_from_disk(pde->rec_len) +
  1525. ext4_rec_len_from_disk(de->rec_len));
  1526. else
  1527. de->inode = 0;
  1528. dir->i_version++;
  1529. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  1530. ext4_journal_dirty_metadata(handle, bh);
  1531. return 0;
  1532. }
  1533. i += ext4_rec_len_from_disk(de->rec_len);
  1534. pde = de;
  1535. de = ext4_next_entry(de);
  1536. }
  1537. return -ENOENT;
  1538. }
  1539. /*
  1540. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1541. * since this indicates that nlinks count was previously 1.
  1542. */
  1543. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1544. {
  1545. inc_nlink(inode);
  1546. if (is_dx(inode) && inode->i_nlink > 1) {
  1547. /* limit is 16-bit i_links_count */
  1548. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1549. inode->i_nlink = 1;
  1550. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1551. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1552. }
  1553. }
  1554. }
  1555. /*
  1556. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1557. * directory has >EXT4_LINK_MAX subdirs.
  1558. */
  1559. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1560. {
  1561. drop_nlink(inode);
  1562. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1563. inc_nlink(inode);
  1564. }
  1565. static int ext4_add_nondir(handle_t *handle,
  1566. struct dentry *dentry, struct inode *inode)
  1567. {
  1568. int err = ext4_add_entry(handle, dentry, inode);
  1569. if (!err) {
  1570. ext4_mark_inode_dirty(handle, inode);
  1571. d_instantiate(dentry, inode);
  1572. return 0;
  1573. }
  1574. drop_nlink(inode);
  1575. iput(inode);
  1576. return err;
  1577. }
  1578. /*
  1579. * By the time this is called, we already have created
  1580. * the directory cache entry for the new file, but it
  1581. * is so far negative - it has no inode.
  1582. *
  1583. * If the create succeeds, we fill in the inode information
  1584. * with d_instantiate().
  1585. */
  1586. static int ext4_create (struct inode * dir, struct dentry * dentry, int mode,
  1587. struct nameidata *nd)
  1588. {
  1589. handle_t *handle;
  1590. struct inode * inode;
  1591. int err, retries = 0;
  1592. retry:
  1593. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1594. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1595. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1596. if (IS_ERR(handle))
  1597. return PTR_ERR(handle);
  1598. if (IS_DIRSYNC(dir))
  1599. handle->h_sync = 1;
  1600. inode = ext4_new_inode (handle, dir, mode);
  1601. err = PTR_ERR(inode);
  1602. if (!IS_ERR(inode)) {
  1603. inode->i_op = &ext4_file_inode_operations;
  1604. inode->i_fop = &ext4_file_operations;
  1605. ext4_set_aops(inode);
  1606. err = ext4_add_nondir(handle, dentry, inode);
  1607. }
  1608. ext4_journal_stop(handle);
  1609. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1610. goto retry;
  1611. return err;
  1612. }
  1613. static int ext4_mknod (struct inode * dir, struct dentry *dentry,
  1614. int mode, dev_t rdev)
  1615. {
  1616. handle_t *handle;
  1617. struct inode *inode;
  1618. int err, retries = 0;
  1619. if (!new_valid_dev(rdev))
  1620. return -EINVAL;
  1621. retry:
  1622. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1623. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1624. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1625. if (IS_ERR(handle))
  1626. return PTR_ERR(handle);
  1627. if (IS_DIRSYNC(dir))
  1628. handle->h_sync = 1;
  1629. inode = ext4_new_inode (handle, dir, mode);
  1630. err = PTR_ERR(inode);
  1631. if (!IS_ERR(inode)) {
  1632. init_special_inode(inode, inode->i_mode, rdev);
  1633. #ifdef CONFIG_EXT4DEV_FS_XATTR
  1634. inode->i_op = &ext4_special_inode_operations;
  1635. #endif
  1636. err = ext4_add_nondir(handle, dentry, inode);
  1637. }
  1638. ext4_journal_stop(handle);
  1639. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1640. goto retry;
  1641. return err;
  1642. }
  1643. static int ext4_mkdir(struct inode * dir, struct dentry * dentry, int mode)
  1644. {
  1645. handle_t *handle;
  1646. struct inode * inode;
  1647. struct buffer_head * dir_block;
  1648. struct ext4_dir_entry_2 * de;
  1649. int err, retries = 0;
  1650. if (EXT4_DIR_LINK_MAX(dir))
  1651. return -EMLINK;
  1652. retry:
  1653. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1654. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1655. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1656. if (IS_ERR(handle))
  1657. return PTR_ERR(handle);
  1658. if (IS_DIRSYNC(dir))
  1659. handle->h_sync = 1;
  1660. inode = ext4_new_inode (handle, dir, S_IFDIR | mode);
  1661. err = PTR_ERR(inode);
  1662. if (IS_ERR(inode))
  1663. goto out_stop;
  1664. inode->i_op = &ext4_dir_inode_operations;
  1665. inode->i_fop = &ext4_dir_operations;
  1666. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1667. dir_block = ext4_bread (handle, inode, 0, 1, &err);
  1668. if (!dir_block) {
  1669. ext4_dec_count(handle, inode); /* is this nlink == 0? */
  1670. ext4_mark_inode_dirty(handle, inode);
  1671. iput (inode);
  1672. goto out_stop;
  1673. }
  1674. BUFFER_TRACE(dir_block, "get_write_access");
  1675. ext4_journal_get_write_access(handle, dir_block);
  1676. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1677. de->inode = cpu_to_le32(inode->i_ino);
  1678. de->name_len = 1;
  1679. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
  1680. strcpy (de->name, ".");
  1681. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1682. de = ext4_next_entry(de);
  1683. de->inode = cpu_to_le32(dir->i_ino);
  1684. de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
  1685. EXT4_DIR_REC_LEN(1));
  1686. de->name_len = 2;
  1687. strcpy (de->name, "..");
  1688. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1689. inode->i_nlink = 2;
  1690. BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
  1691. ext4_journal_dirty_metadata(handle, dir_block);
  1692. brelse (dir_block);
  1693. ext4_mark_inode_dirty(handle, inode);
  1694. err = ext4_add_entry (handle, dentry, inode);
  1695. if (err) {
  1696. inode->i_nlink = 0;
  1697. ext4_mark_inode_dirty(handle, inode);
  1698. iput (inode);
  1699. goto out_stop;
  1700. }
  1701. ext4_inc_count(handle, dir);
  1702. ext4_update_dx_flag(dir);
  1703. ext4_mark_inode_dirty(handle, dir);
  1704. d_instantiate(dentry, inode);
  1705. out_stop:
  1706. ext4_journal_stop(handle);
  1707. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1708. goto retry;
  1709. return err;
  1710. }
  1711. /*
  1712. * routine to check that the specified directory is empty (for rmdir)
  1713. */
  1714. static int empty_dir (struct inode * inode)
  1715. {
  1716. unsigned long offset;
  1717. struct buffer_head * bh;
  1718. struct ext4_dir_entry_2 * de, * de1;
  1719. struct super_block * sb;
  1720. int err = 0;
  1721. sb = inode->i_sb;
  1722. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1723. !(bh = ext4_bread (NULL, inode, 0, 0, &err))) {
  1724. if (err)
  1725. ext4_error(inode->i_sb, __FUNCTION__,
  1726. "error %d reading directory #%lu offset 0",
  1727. err, inode->i_ino);
  1728. else
  1729. ext4_warning(inode->i_sb, __FUNCTION__,
  1730. "bad directory (dir #%lu) - no data block",
  1731. inode->i_ino);
  1732. return 1;
  1733. }
  1734. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1735. de1 = ext4_next_entry(de);
  1736. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1737. !le32_to_cpu(de1->inode) ||
  1738. strcmp (".", de->name) ||
  1739. strcmp ("..", de1->name)) {
  1740. ext4_warning (inode->i_sb, "empty_dir",
  1741. "bad directory (dir #%lu) - no `.' or `..'",
  1742. inode->i_ino);
  1743. brelse (bh);
  1744. return 1;
  1745. }
  1746. offset = ext4_rec_len_from_disk(de->rec_len) +
  1747. ext4_rec_len_from_disk(de1->rec_len);
  1748. de = ext4_next_entry(de1);
  1749. while (offset < inode->i_size ) {
  1750. if (!bh ||
  1751. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1752. err = 0;
  1753. brelse (bh);
  1754. bh = ext4_bread (NULL, inode,
  1755. offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
  1756. if (!bh) {
  1757. if (err)
  1758. ext4_error(sb, __FUNCTION__,
  1759. "error %d reading directory"
  1760. " #%lu offset %lu",
  1761. err, inode->i_ino, offset);
  1762. offset += sb->s_blocksize;
  1763. continue;
  1764. }
  1765. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1766. }
  1767. if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1768. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1769. sb->s_blocksize);
  1770. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1771. continue;
  1772. }
  1773. if (le32_to_cpu(de->inode)) {
  1774. brelse (bh);
  1775. return 0;
  1776. }
  1777. offset += ext4_rec_len_from_disk(de->rec_len);
  1778. de = ext4_next_entry(de);
  1779. }
  1780. brelse (bh);
  1781. return 1;
  1782. }
  1783. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1784. * such inodes, starting at the superblock, in case we crash before the
  1785. * file is closed/deleted, or in case the inode truncate spans multiple
  1786. * transactions and the last transaction is not recovered after a crash.
  1787. *
  1788. * At filesystem recovery time, we walk this list deleting unlinked
  1789. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1790. */
  1791. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1792. {
  1793. struct super_block *sb = inode->i_sb;
  1794. struct ext4_iloc iloc;
  1795. int err = 0, rc;
  1796. lock_super(sb);
  1797. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1798. goto out_unlock;
  1799. /* Orphan handling is only valid for files with data blocks
  1800. * being truncated, or files being unlinked. */
  1801. /* @@@ FIXME: Observation from aviro:
  1802. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1803. * here (on lock_super()), so race with ext4_link() which might bump
  1804. * ->i_nlink. For, say it, character device. Not a regular file,
  1805. * not a directory, not a symlink and ->i_nlink > 0.
  1806. */
  1807. J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1808. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1809. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1810. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1811. if (err)
  1812. goto out_unlock;
  1813. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1814. if (err)
  1815. goto out_unlock;
  1816. /* Insert this inode at the head of the on-disk orphan list... */
  1817. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1818. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1819. err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  1820. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1821. if (!err)
  1822. err = rc;
  1823. /* Only add to the head of the in-memory list if all the
  1824. * previous operations succeeded. If the orphan_add is going to
  1825. * fail (possibly taking the journal offline), we can't risk
  1826. * leaving the inode on the orphan list: stray orphan-list
  1827. * entries can cause panics at unmount time.
  1828. *
  1829. * This is safe: on error we're going to ignore the orphan list
  1830. * anyway on the next recovery. */
  1831. if (!err)
  1832. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1833. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1834. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1835. inode->i_ino, NEXT_ORPHAN(inode));
  1836. out_unlock:
  1837. unlock_super(sb);
  1838. ext4_std_error(inode->i_sb, err);
  1839. return err;
  1840. }
  1841. /*
  1842. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1843. * of such inodes stored on disk, because it is finally being cleaned up.
  1844. */
  1845. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1846. {
  1847. struct list_head *prev;
  1848. struct ext4_inode_info *ei = EXT4_I(inode);
  1849. struct ext4_sb_info *sbi;
  1850. unsigned long ino_next;
  1851. struct ext4_iloc iloc;
  1852. int err = 0;
  1853. lock_super(inode->i_sb);
  1854. if (list_empty(&ei->i_orphan)) {
  1855. unlock_super(inode->i_sb);
  1856. return 0;
  1857. }
  1858. ino_next = NEXT_ORPHAN(inode);
  1859. prev = ei->i_orphan.prev;
  1860. sbi = EXT4_SB(inode->i_sb);
  1861. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1862. list_del_init(&ei->i_orphan);
  1863. /* If we're on an error path, we may not have a valid
  1864. * transaction handle with which to update the orphan list on
  1865. * disk, but we still need to remove the inode from the linked
  1866. * list in memory. */
  1867. if (!handle)
  1868. goto out;
  1869. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1870. if (err)
  1871. goto out_err;
  1872. if (prev == &sbi->s_orphan) {
  1873. jbd_debug(4, "superblock will point to %lu\n", ino_next);
  1874. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1875. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1876. if (err)
  1877. goto out_brelse;
  1878. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1879. err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
  1880. } else {
  1881. struct ext4_iloc iloc2;
  1882. struct inode *i_prev =
  1883. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1884. jbd_debug(4, "orphan inode %lu will point to %lu\n",
  1885. i_prev->i_ino, ino_next);
  1886. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1887. if (err)
  1888. goto out_brelse;
  1889. NEXT_ORPHAN(i_prev) = ino_next;
  1890. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1891. }
  1892. if (err)
  1893. goto out_brelse;
  1894. NEXT_ORPHAN(inode) = 0;
  1895. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1896. out_err:
  1897. ext4_std_error(inode->i_sb, err);
  1898. out:
  1899. unlock_super(inode->i_sb);
  1900. return err;
  1901. out_brelse:
  1902. brelse(iloc.bh);
  1903. goto out_err;
  1904. }
  1905. static int ext4_rmdir (struct inode * dir, struct dentry *dentry)
  1906. {
  1907. int retval;
  1908. struct inode * inode;
  1909. struct buffer_head * bh;
  1910. struct ext4_dir_entry_2 * de;
  1911. handle_t *handle;
  1912. /* Initialize quotas before so that eventual writes go in
  1913. * separate transaction */
  1914. DQUOT_INIT(dentry->d_inode);
  1915. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1916. if (IS_ERR(handle))
  1917. return PTR_ERR(handle);
  1918. retval = -ENOENT;
  1919. bh = ext4_find_entry (dentry, &de);
  1920. if (!bh)
  1921. goto end_rmdir;
  1922. if (IS_DIRSYNC(dir))
  1923. handle->h_sync = 1;
  1924. inode = dentry->d_inode;
  1925. retval = -EIO;
  1926. if (le32_to_cpu(de->inode) != inode->i_ino)
  1927. goto end_rmdir;
  1928. retval = -ENOTEMPTY;
  1929. if (!empty_dir (inode))
  1930. goto end_rmdir;
  1931. retval = ext4_delete_entry(handle, dir, de, bh);
  1932. if (retval)
  1933. goto end_rmdir;
  1934. if (!EXT4_DIR_LINK_EMPTY(inode))
  1935. ext4_warning (inode->i_sb, "ext4_rmdir",
  1936. "empty directory has too many links (%d)",
  1937. inode->i_nlink);
  1938. inode->i_version++;
  1939. clear_nlink(inode);
  1940. /* There's no need to set i_disksize: the fact that i_nlink is
  1941. * zero will ensure that the right thing happens during any
  1942. * recovery. */
  1943. inode->i_size = 0;
  1944. ext4_orphan_add(handle, inode);
  1945. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1946. ext4_mark_inode_dirty(handle, inode);
  1947. ext4_dec_count(handle, dir);
  1948. ext4_update_dx_flag(dir);
  1949. ext4_mark_inode_dirty(handle, dir);
  1950. end_rmdir:
  1951. ext4_journal_stop(handle);
  1952. brelse (bh);
  1953. return retval;
  1954. }
  1955. static int ext4_unlink(struct inode * dir, struct dentry *dentry)
  1956. {
  1957. int retval;
  1958. struct inode * inode;
  1959. struct buffer_head * bh;
  1960. struct ext4_dir_entry_2 * de;
  1961. handle_t *handle;
  1962. /* Initialize quotas before so that eventual writes go
  1963. * in separate transaction */
  1964. DQUOT_INIT(dentry->d_inode);
  1965. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1966. if (IS_ERR(handle))
  1967. return PTR_ERR(handle);
  1968. if (IS_DIRSYNC(dir))
  1969. handle->h_sync = 1;
  1970. retval = -ENOENT;
  1971. bh = ext4_find_entry (dentry, &de);
  1972. if (!bh)
  1973. goto end_unlink;
  1974. inode = dentry->d_inode;
  1975. retval = -EIO;
  1976. if (le32_to_cpu(de->inode) != inode->i_ino)
  1977. goto end_unlink;
  1978. if (!inode->i_nlink) {
  1979. ext4_warning (inode->i_sb, "ext4_unlink",
  1980. "Deleting nonexistent file (%lu), %d",
  1981. inode->i_ino, inode->i_nlink);
  1982. inode->i_nlink = 1;
  1983. }
  1984. retval = ext4_delete_entry(handle, dir, de, bh);
  1985. if (retval)
  1986. goto end_unlink;
  1987. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  1988. ext4_update_dx_flag(dir);
  1989. ext4_mark_inode_dirty(handle, dir);
  1990. ext4_dec_count(handle, inode);
  1991. if (!inode->i_nlink)
  1992. ext4_orphan_add(handle, inode);
  1993. inode->i_ctime = ext4_current_time(inode);
  1994. ext4_mark_inode_dirty(handle, inode);
  1995. retval = 0;
  1996. end_unlink:
  1997. ext4_journal_stop(handle);
  1998. brelse (bh);
  1999. return retval;
  2000. }
  2001. static int ext4_symlink (struct inode * dir,
  2002. struct dentry *dentry, const char * symname)
  2003. {
  2004. handle_t *handle;
  2005. struct inode * inode;
  2006. int l, err, retries = 0;
  2007. l = strlen(symname)+1;
  2008. if (l > dir->i_sb->s_blocksize)
  2009. return -ENAMETOOLONG;
  2010. retry:
  2011. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2012. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  2013. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  2014. if (IS_ERR(handle))
  2015. return PTR_ERR(handle);
  2016. if (IS_DIRSYNC(dir))
  2017. handle->h_sync = 1;
  2018. inode = ext4_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
  2019. err = PTR_ERR(inode);
  2020. if (IS_ERR(inode))
  2021. goto out_stop;
  2022. if (l > sizeof (EXT4_I(inode)->i_data)) {
  2023. inode->i_op = &ext4_symlink_inode_operations;
  2024. ext4_set_aops(inode);
  2025. /*
  2026. * page_symlink() calls into ext4_prepare/commit_write.
  2027. * We have a transaction open. All is sweetness. It also sets
  2028. * i_size in generic_commit_write().
  2029. */
  2030. err = __page_symlink(inode, symname, l,
  2031. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  2032. if (err) {
  2033. ext4_dec_count(handle, inode);
  2034. ext4_mark_inode_dirty(handle, inode);
  2035. iput (inode);
  2036. goto out_stop;
  2037. }
  2038. } else {
  2039. inode->i_op = &ext4_fast_symlink_inode_operations;
  2040. memcpy((char*)&EXT4_I(inode)->i_data,symname,l);
  2041. inode->i_size = l-1;
  2042. }
  2043. EXT4_I(inode)->i_disksize = inode->i_size;
  2044. err = ext4_add_nondir(handle, dentry, inode);
  2045. out_stop:
  2046. ext4_journal_stop(handle);
  2047. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2048. goto retry;
  2049. return err;
  2050. }
  2051. static int ext4_link (struct dentry * old_dentry,
  2052. struct inode * dir, struct dentry *dentry)
  2053. {
  2054. handle_t *handle;
  2055. struct inode *inode = old_dentry->d_inode;
  2056. int err, retries = 0;
  2057. if (EXT4_DIR_LINK_MAX(inode))
  2058. return -EMLINK;
  2059. /*
  2060. * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
  2061. * otherwise has the potential to corrupt the orphan inode list.
  2062. */
  2063. if (inode->i_nlink == 0)
  2064. return -ENOENT;
  2065. retry:
  2066. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2067. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2068. if (IS_ERR(handle))
  2069. return PTR_ERR(handle);
  2070. if (IS_DIRSYNC(dir))
  2071. handle->h_sync = 1;
  2072. inode->i_ctime = ext4_current_time(inode);
  2073. ext4_inc_count(handle, inode);
  2074. atomic_inc(&inode->i_count);
  2075. err = ext4_add_nondir(handle, dentry, inode);
  2076. ext4_journal_stop(handle);
  2077. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2078. goto retry;
  2079. return err;
  2080. }
  2081. #define PARENT_INO(buffer) \
  2082. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
  2083. /*
  2084. * Anybody can rename anything with this: the permission checks are left to the
  2085. * higher-level routines.
  2086. */
  2087. static int ext4_rename (struct inode * old_dir, struct dentry *old_dentry,
  2088. struct inode * new_dir,struct dentry *new_dentry)
  2089. {
  2090. handle_t *handle;
  2091. struct inode * old_inode, * new_inode;
  2092. struct buffer_head * old_bh, * new_bh, * dir_bh;
  2093. struct ext4_dir_entry_2 * old_de, * new_de;
  2094. int retval;
  2095. old_bh = new_bh = dir_bh = NULL;
  2096. /* Initialize quotas before so that eventual writes go
  2097. * in separate transaction */
  2098. if (new_dentry->d_inode)
  2099. DQUOT_INIT(new_dentry->d_inode);
  2100. handle = ext4_journal_start(old_dir, 2 *
  2101. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2102. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2103. if (IS_ERR(handle))
  2104. return PTR_ERR(handle);
  2105. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2106. handle->h_sync = 1;
  2107. old_bh = ext4_find_entry (old_dentry, &old_de);
  2108. /*
  2109. * Check for inode number is _not_ due to possible IO errors.
  2110. * We might rmdir the source, keep it as pwd of some process
  2111. * and merrily kill the link to whatever was created under the
  2112. * same name. Goodbye sticky bit ;-<
  2113. */
  2114. old_inode = old_dentry->d_inode;
  2115. retval = -ENOENT;
  2116. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2117. goto end_rename;
  2118. new_inode = new_dentry->d_inode;
  2119. new_bh = ext4_find_entry (new_dentry, &new_de);
  2120. if (new_bh) {
  2121. if (!new_inode) {
  2122. brelse (new_bh);
  2123. new_bh = NULL;
  2124. }
  2125. }
  2126. if (S_ISDIR(old_inode->i_mode)) {
  2127. if (new_inode) {
  2128. retval = -ENOTEMPTY;
  2129. if (!empty_dir (new_inode))
  2130. goto end_rename;
  2131. }
  2132. retval = -EIO;
  2133. dir_bh = ext4_bread (handle, old_inode, 0, 0, &retval);
  2134. if (!dir_bh)
  2135. goto end_rename;
  2136. if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
  2137. goto end_rename;
  2138. retval = -EMLINK;
  2139. if (!new_inode && new_dir!=old_dir &&
  2140. new_dir->i_nlink >= EXT4_LINK_MAX)
  2141. goto end_rename;
  2142. }
  2143. if (!new_bh) {
  2144. retval = ext4_add_entry (handle, new_dentry, old_inode);
  2145. if (retval)
  2146. goto end_rename;
  2147. } else {
  2148. BUFFER_TRACE(new_bh, "get write access");
  2149. ext4_journal_get_write_access(handle, new_bh);
  2150. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2151. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2152. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2153. new_de->file_type = old_de->file_type;
  2154. new_dir->i_version++;
  2155. BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
  2156. ext4_journal_dirty_metadata(handle, new_bh);
  2157. brelse(new_bh);
  2158. new_bh = NULL;
  2159. }
  2160. /*
  2161. * Like most other Unix systems, set the ctime for inodes on a
  2162. * rename.
  2163. */
  2164. old_inode->i_ctime = ext4_current_time(old_inode);
  2165. ext4_mark_inode_dirty(handle, old_inode);
  2166. /*
  2167. * ok, that's it
  2168. */
  2169. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2170. old_de->name_len != old_dentry->d_name.len ||
  2171. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2172. (retval = ext4_delete_entry(handle, old_dir,
  2173. old_de, old_bh)) == -ENOENT) {
  2174. /* old_de could have moved from under us during htree split, so
  2175. * make sure that we are deleting the right entry. We might
  2176. * also be pointing to a stale entry in the unused part of
  2177. * old_bh so just checking inum and the name isn't enough. */
  2178. struct buffer_head *old_bh2;
  2179. struct ext4_dir_entry_2 *old_de2;
  2180. old_bh2 = ext4_find_entry(old_dentry, &old_de2);
  2181. if (old_bh2) {
  2182. retval = ext4_delete_entry(handle, old_dir,
  2183. old_de2, old_bh2);
  2184. brelse(old_bh2);
  2185. }
  2186. }
  2187. if (retval) {
  2188. ext4_warning(old_dir->i_sb, "ext4_rename",
  2189. "Deleting old file (%lu), %d, error=%d",
  2190. old_dir->i_ino, old_dir->i_nlink, retval);
  2191. }
  2192. if (new_inode) {
  2193. ext4_dec_count(handle, new_inode);
  2194. new_inode->i_ctime = ext4_current_time(new_inode);
  2195. }
  2196. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2197. ext4_update_dx_flag(old_dir);
  2198. if (dir_bh) {
  2199. BUFFER_TRACE(dir_bh, "get_write_access");
  2200. ext4_journal_get_write_access(handle, dir_bh);
  2201. PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
  2202. BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
  2203. ext4_journal_dirty_metadata(handle, dir_bh);
  2204. ext4_dec_count(handle, old_dir);
  2205. if (new_inode) {
  2206. /* checked empty_dir above, can't have another parent,
  2207. * ext3_dec_count() won't work for many-linked dirs */
  2208. new_inode->i_nlink = 0;
  2209. } else {
  2210. ext4_inc_count(handle, new_dir);
  2211. ext4_update_dx_flag(new_dir);
  2212. ext4_mark_inode_dirty(handle, new_dir);
  2213. }
  2214. }
  2215. ext4_mark_inode_dirty(handle, old_dir);
  2216. if (new_inode) {
  2217. ext4_mark_inode_dirty(handle, new_inode);
  2218. if (!new_inode->i_nlink)
  2219. ext4_orphan_add(handle, new_inode);
  2220. }
  2221. retval = 0;
  2222. end_rename:
  2223. brelse (dir_bh);
  2224. brelse (old_bh);
  2225. brelse (new_bh);
  2226. ext4_journal_stop(handle);
  2227. return retval;
  2228. }
  2229. /*
  2230. * directories can handle most operations...
  2231. */
  2232. const struct inode_operations ext4_dir_inode_operations = {
  2233. .create = ext4_create,
  2234. .lookup = ext4_lookup,
  2235. .link = ext4_link,
  2236. .unlink = ext4_unlink,
  2237. .symlink = ext4_symlink,
  2238. .mkdir = ext4_mkdir,
  2239. .rmdir = ext4_rmdir,
  2240. .mknod = ext4_mknod,
  2241. .rename = ext4_rename,
  2242. .setattr = ext4_setattr,
  2243. #ifdef CONFIG_EXT4DEV_FS_XATTR
  2244. .setxattr = generic_setxattr,
  2245. .getxattr = generic_getxattr,
  2246. .listxattr = ext4_listxattr,
  2247. .removexattr = generic_removexattr,
  2248. #endif
  2249. .permission = ext4_permission,
  2250. };
  2251. const struct inode_operations ext4_special_inode_operations = {
  2252. .setattr = ext4_setattr,
  2253. #ifdef CONFIG_EXT4DEV_FS_XATTR
  2254. .setxattr = generic_setxattr,
  2255. .getxattr = generic_getxattr,
  2256. .listxattr = ext4_listxattr,
  2257. .removexattr = generic_removexattr,
  2258. #endif
  2259. .permission = ext4_permission,
  2260. };