namei.c 66 KB

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