namei.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  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 "xattr.h"
  39. #include "acl.h"
  40. #include <trace/events/ext4.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, unsigned blocksize,
  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, unsigned blocksize);
  151. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  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, unsigned long blocksize)
  169. {
  170. return (struct ext4_dir_entry_2 *)((char *)p +
  171. ext4_rec_len_from_disk(p->rec_len, blocksize));
  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. (unsigned) ((char *) de - base));
  259. }
  260. space += EXT4_DIR_REC_LEN(de->name_len);
  261. names++;
  262. }
  263. de = ext4_next_entry(de, size);
  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, "Unrecognised inode hash code %d",
  327. root->info.hash_version);
  328. brelse(bh);
  329. *err = ERR_BAD_DX_DIR;
  330. goto fail;
  331. }
  332. hinfo->hash_version = root->info.hash_version;
  333. if (hinfo->hash_version <= DX_HASH_TEA)
  334. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  335. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  336. if (d_name)
  337. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  338. hash = hinfo->hash;
  339. if (root->info.unused_flags & 1) {
  340. ext4_warning(dir->i_sb, "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, "Unimplemented inode hash depth: %#06x",
  348. root->info.indirect_levels);
  349. brelse(bh);
  350. *err = ERR_BAD_DX_DIR;
  351. goto fail;
  352. }
  353. entries = (struct dx_entry *) (((char *)&root->info) +
  354. root->info.info_length);
  355. if (dx_get_limit(entries) != dx_root_limit(dir,
  356. root->info.info_length)) {
  357. ext4_warning(dir->i_sb, "dx entry: limit != root limit");
  358. brelse(bh);
  359. *err = ERR_BAD_DX_DIR;
  360. goto fail;
  361. }
  362. dxtrace(printk("Look up %x", hash));
  363. while (1)
  364. {
  365. count = dx_get_count(entries);
  366. if (!count || count > dx_get_limit(entries)) {
  367. ext4_warning(dir->i_sb,
  368. "dx entry: no count or count > limit");
  369. brelse(bh);
  370. *err = ERR_BAD_DX_DIR;
  371. goto fail2;
  372. }
  373. p = entries + 1;
  374. q = entries + count - 1;
  375. while (p <= q)
  376. {
  377. m = p + (q - p)/2;
  378. dxtrace(printk("."));
  379. if (dx_get_hash(m) > hash)
  380. q = m - 1;
  381. else
  382. p = m + 1;
  383. }
  384. if (0) // linear search cross check
  385. {
  386. unsigned n = count - 1;
  387. at = entries;
  388. while (n--)
  389. {
  390. dxtrace(printk(","));
  391. if (dx_get_hash(++at) > hash)
  392. {
  393. at--;
  394. break;
  395. }
  396. }
  397. assert (at == p - 1);
  398. }
  399. at = p - 1;
  400. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  401. frame->bh = bh;
  402. frame->entries = entries;
  403. frame->at = at;
  404. if (!indirect--) return frame;
  405. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  406. goto fail2;
  407. at = entries = ((struct dx_node *) bh->b_data)->entries;
  408. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  409. ext4_warning(dir->i_sb,
  410. "dx entry: limit != node limit");
  411. brelse(bh);
  412. *err = ERR_BAD_DX_DIR;
  413. goto fail2;
  414. }
  415. frame++;
  416. frame->bh = NULL;
  417. }
  418. fail2:
  419. while (frame >= frame_in) {
  420. brelse(frame->bh);
  421. frame--;
  422. }
  423. fail:
  424. if (*err == ERR_BAD_DX_DIR)
  425. ext4_warning(dir->i_sb,
  426. "Corrupt dir inode %ld, running e2fsck is "
  427. "recommended.", dir->i_ino);
  428. return NULL;
  429. }
  430. static void dx_release (struct dx_frame *frames)
  431. {
  432. if (frames[0].bh == NULL)
  433. return;
  434. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  435. brelse(frames[1].bh);
  436. brelse(frames[0].bh);
  437. }
  438. /*
  439. * This function increments the frame pointer to search the next leaf
  440. * block, and reads in the necessary intervening nodes if the search
  441. * should be necessary. Whether or not the search is necessary is
  442. * controlled by the hash parameter. If the hash value is even, then
  443. * the search is only continued if the next block starts with that
  444. * hash value. This is used if we are searching for a specific file.
  445. *
  446. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  447. *
  448. * This function returns 1 if the caller should continue to search,
  449. * or 0 if it should not. If there is an error reading one of the
  450. * index blocks, it will a negative error code.
  451. *
  452. * If start_hash is non-null, it will be filled in with the starting
  453. * hash of the next page.
  454. */
  455. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  456. struct dx_frame *frame,
  457. struct dx_frame *frames,
  458. __u32 *start_hash)
  459. {
  460. struct dx_frame *p;
  461. struct buffer_head *bh;
  462. int err, num_frames = 0;
  463. __u32 bhash;
  464. p = frame;
  465. /*
  466. * Find the next leaf page by incrementing the frame pointer.
  467. * If we run out of entries in the interior node, loop around and
  468. * increment pointer in the parent node. When we break out of
  469. * this loop, num_frames indicates the number of interior
  470. * nodes need to be read.
  471. */
  472. while (1) {
  473. if (++(p->at) < p->entries + dx_get_count(p->entries))
  474. break;
  475. if (p == frames)
  476. return 0;
  477. num_frames++;
  478. p--;
  479. }
  480. /*
  481. * If the hash is 1, then continue only if the next page has a
  482. * continuation hash of any value. This is used for readdir
  483. * handling. Otherwise, check to see if the hash matches the
  484. * desired contiuation hash. If it doesn't, return since
  485. * there's no point to read in the successive index pages.
  486. */
  487. bhash = dx_get_hash(p->at);
  488. if (start_hash)
  489. *start_hash = bhash;
  490. if ((hash & 1) == 0) {
  491. if ((bhash & ~1) != hash)
  492. return 0;
  493. }
  494. /*
  495. * If the hash is HASH_NB_ALWAYS, we always go to the next
  496. * block so no check is necessary
  497. */
  498. while (num_frames--) {
  499. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  500. 0, &err)))
  501. return err; /* Failure */
  502. p++;
  503. brelse(p->bh);
  504. p->bh = bh;
  505. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  506. }
  507. return 1;
  508. }
  509. /*
  510. * This function fills a red-black tree with information from a
  511. * directory block. It returns the number directory entries loaded
  512. * into the tree. If there is an error it is returned in err.
  513. */
  514. static int htree_dirblock_to_tree(struct file *dir_file,
  515. struct inode *dir, ext4_lblk_t block,
  516. struct dx_hash_info *hinfo,
  517. __u32 start_hash, __u32 start_minor_hash)
  518. {
  519. struct buffer_head *bh;
  520. struct ext4_dir_entry_2 *de, *top;
  521. int err, count = 0;
  522. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  523. (unsigned long)block));
  524. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  525. return err;
  526. de = (struct ext4_dir_entry_2 *) bh->b_data;
  527. top = (struct ext4_dir_entry_2 *) ((char *) de +
  528. dir->i_sb->s_blocksize -
  529. EXT4_DIR_REC_LEN(0));
  530. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  531. if (ext4_check_dir_entry(dir, NULL, de, bh,
  532. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  533. + ((char *)de - bh->b_data))) {
  534. /* On error, skip the f_pos to the next block. */
  535. dir_file->f_pos = (dir_file->f_pos |
  536. (dir->i_sb->s_blocksize - 1)) + 1;
  537. brelse(bh);
  538. return count;
  539. }
  540. ext4fs_dirhash(de->name, de->name_len, hinfo);
  541. if ((hinfo->hash < start_hash) ||
  542. ((hinfo->hash == start_hash) &&
  543. (hinfo->minor_hash < start_minor_hash)))
  544. continue;
  545. if (de->inode == 0)
  546. continue;
  547. if ((err = ext4_htree_store_dirent(dir_file,
  548. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  549. brelse(bh);
  550. return err;
  551. }
  552. count++;
  553. }
  554. brelse(bh);
  555. return count;
  556. }
  557. /*
  558. * This function fills a red-black tree with information from a
  559. * directory. We start scanning the directory in hash order, starting
  560. * at start_hash and start_minor_hash.
  561. *
  562. * This function returns the number of entries inserted into the tree,
  563. * or a negative error code.
  564. */
  565. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  566. __u32 start_minor_hash, __u32 *next_hash)
  567. {
  568. struct dx_hash_info hinfo;
  569. struct ext4_dir_entry_2 *de;
  570. struct dx_frame frames[2], *frame;
  571. struct inode *dir;
  572. ext4_lblk_t block;
  573. int count = 0;
  574. int ret, err;
  575. __u32 hashval;
  576. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  577. start_hash, start_minor_hash));
  578. dir = dir_file->f_path.dentry->d_inode;
  579. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  580. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  581. if (hinfo.hash_version <= DX_HASH_TEA)
  582. hinfo.hash_version +=
  583. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  584. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  585. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  586. start_hash, start_minor_hash);
  587. *next_hash = ~0;
  588. return count;
  589. }
  590. hinfo.hash = start_hash;
  591. hinfo.minor_hash = 0;
  592. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  593. if (!frame)
  594. return err;
  595. /* Add '.' and '..' from the htree header */
  596. if (!start_hash && !start_minor_hash) {
  597. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  598. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  599. goto errout;
  600. count++;
  601. }
  602. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  603. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  604. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  605. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  606. goto errout;
  607. count++;
  608. }
  609. while (1) {
  610. block = dx_get_block(frame->at);
  611. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  612. start_hash, start_minor_hash);
  613. if (ret < 0) {
  614. err = ret;
  615. goto errout;
  616. }
  617. count += ret;
  618. hashval = ~0;
  619. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  620. frame, frames, &hashval);
  621. *next_hash = hashval;
  622. if (ret < 0) {
  623. err = ret;
  624. goto errout;
  625. }
  626. /*
  627. * Stop if: (a) there are no more entries, or
  628. * (b) we have inserted at least one entry and the
  629. * next hash value is not a continuation
  630. */
  631. if ((ret == 0) ||
  632. (count && ((hashval & 1) == 0)))
  633. break;
  634. }
  635. dx_release(frames);
  636. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  637. "next hash: %x\n", count, *next_hash));
  638. return count;
  639. errout:
  640. dx_release(frames);
  641. return (err);
  642. }
  643. /*
  644. * Directory block splitting, compacting
  645. */
  646. /*
  647. * Create map of hash values, offsets, and sizes, stored at end of block.
  648. * Returns number of entries mapped.
  649. */
  650. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  651. struct dx_hash_info *hinfo,
  652. struct dx_map_entry *map_tail)
  653. {
  654. int count = 0;
  655. char *base = (char *) de;
  656. struct dx_hash_info h = *hinfo;
  657. while ((char *) de < base + blocksize) {
  658. if (de->name_len && de->inode) {
  659. ext4fs_dirhash(de->name, de->name_len, &h);
  660. map_tail--;
  661. map_tail->hash = h.hash;
  662. map_tail->offs = ((char *) de - base)>>2;
  663. map_tail->size = le16_to_cpu(de->rec_len);
  664. count++;
  665. cond_resched();
  666. }
  667. /* XXX: do we need to check rec_len == 0 case? -Chris */
  668. de = ext4_next_entry(de, blocksize);
  669. }
  670. return count;
  671. }
  672. /* Sort map by hash value */
  673. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  674. {
  675. struct dx_map_entry *p, *q, *top = map + count - 1;
  676. int more;
  677. /* Combsort until bubble sort doesn't suck */
  678. while (count > 2) {
  679. count = count*10/13;
  680. if (count - 9 < 2) /* 9, 10 -> 11 */
  681. count = 11;
  682. for (p = top, q = p - count; q >= map; p--, q--)
  683. if (p->hash < q->hash)
  684. swap(*p, *q);
  685. }
  686. /* Garden variety bubble sort */
  687. do {
  688. more = 0;
  689. q = top;
  690. while (q-- > map) {
  691. if (q[1].hash >= q[0].hash)
  692. continue;
  693. swap(*(q+1), *q);
  694. more = 1;
  695. }
  696. } while(more);
  697. }
  698. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  699. {
  700. struct dx_entry *entries = frame->entries;
  701. struct dx_entry *old = frame->at, *new = old + 1;
  702. int count = dx_get_count(entries);
  703. assert(count < dx_get_limit(entries));
  704. assert(old < entries + count);
  705. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  706. dx_set_hash(new, hash);
  707. dx_set_block(new, block);
  708. dx_set_count(entries, count + 1);
  709. }
  710. static void ext4_update_dx_flag(struct inode *inode)
  711. {
  712. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  713. EXT4_FEATURE_COMPAT_DIR_INDEX))
  714. ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
  715. }
  716. /*
  717. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  718. *
  719. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  720. * `de != NULL' is guaranteed by caller.
  721. */
  722. static inline int ext4_match (int len, const char * const name,
  723. struct ext4_dir_entry_2 * de)
  724. {
  725. if (len != de->name_len)
  726. return 0;
  727. if (!de->inode)
  728. return 0;
  729. return !memcmp(name, de->name, len);
  730. }
  731. /*
  732. * Returns 0 if not found, -1 on failure, and 1 on success
  733. */
  734. static inline int search_dirblock(struct buffer_head *bh,
  735. struct inode *dir,
  736. const struct qstr *d_name,
  737. unsigned int offset,
  738. struct ext4_dir_entry_2 ** res_dir)
  739. {
  740. struct ext4_dir_entry_2 * de;
  741. char * dlimit;
  742. int de_len;
  743. const char *name = d_name->name;
  744. int namelen = d_name->len;
  745. de = (struct ext4_dir_entry_2 *) bh->b_data;
  746. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  747. while ((char *) de < dlimit) {
  748. /* this code is executed quadratically often */
  749. /* do minimal checking `by hand' */
  750. if ((char *) de + namelen <= dlimit &&
  751. ext4_match (namelen, name, de)) {
  752. /* found a match - just to be sure, do a full check */
  753. if (ext4_check_dir_entry(dir, NULL, 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. dir->i_sb->s_blocksize);
  761. if (de_len <= 0)
  762. return -1;
  763. offset += de_len;
  764. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  765. }
  766. return 0;
  767. }
  768. /*
  769. * ext4_find_entry()
  770. *
  771. * finds an entry in the specified directory with the wanted name. It
  772. * returns the cache buffer in which the entry was found, and the entry
  773. * itself (as a parameter - res_dir). It does NOT read the inode of the
  774. * entry - you'll have to do that yourself if you want to.
  775. *
  776. * The returned buffer_head has ->b_count elevated. The caller is expected
  777. * to brelse() it when appropriate.
  778. */
  779. static struct buffer_head * ext4_find_entry (struct inode *dir,
  780. const struct qstr *d_name,
  781. struct ext4_dir_entry_2 ** res_dir)
  782. {
  783. struct super_block *sb;
  784. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  785. struct buffer_head *bh, *ret = NULL;
  786. ext4_lblk_t start, block, b;
  787. const u8 *name = d_name->name;
  788. int ra_max = 0; /* Number of bh's in the readahead
  789. buffer, bh_use[] */
  790. int ra_ptr = 0; /* Current index into readahead
  791. buffer */
  792. int num = 0;
  793. ext4_lblk_t nblocks;
  794. int i, err;
  795. int namelen;
  796. *res_dir = NULL;
  797. sb = dir->i_sb;
  798. namelen = d_name->len;
  799. if (namelen > EXT4_NAME_LEN)
  800. return NULL;
  801. if ((namelen <= 2) && (name[0] == '.') &&
  802. (name[1] == '.' || name[1] == '\0')) {
  803. /*
  804. * "." or ".." will only be in the first block
  805. * NFS may look up ".."; "." should be handled by the VFS
  806. */
  807. block = start = 0;
  808. nblocks = 1;
  809. goto restart;
  810. }
  811. if (is_dx(dir)) {
  812. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  813. /*
  814. * On success, or if the error was file not found,
  815. * return. Otherwise, fall back to doing a search the
  816. * old fashioned way.
  817. */
  818. if (bh || (err != ERR_BAD_DX_DIR))
  819. return bh;
  820. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  821. "falling back\n"));
  822. }
  823. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  824. start = EXT4_I(dir)->i_dir_start_lookup;
  825. if (start >= nblocks)
  826. start = 0;
  827. block = start;
  828. restart:
  829. do {
  830. /*
  831. * We deal with the read-ahead logic here.
  832. */
  833. if (ra_ptr >= ra_max) {
  834. /* Refill the readahead buffer */
  835. ra_ptr = 0;
  836. b = block;
  837. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  838. /*
  839. * Terminate if we reach the end of the
  840. * directory and must wrap, or if our
  841. * search has finished at this block.
  842. */
  843. if (b >= nblocks || (num && block == start)) {
  844. bh_use[ra_max] = NULL;
  845. break;
  846. }
  847. num++;
  848. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  849. bh_use[ra_max] = bh;
  850. if (bh)
  851. ll_rw_block(READ_META, 1, &bh);
  852. }
  853. }
  854. if ((bh = bh_use[ra_ptr++]) == NULL)
  855. goto next;
  856. wait_on_buffer(bh);
  857. if (!buffer_uptodate(bh)) {
  858. /* read error, skip block & hope for the best */
  859. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  860. (unsigned long) block);
  861. brelse(bh);
  862. goto next;
  863. }
  864. i = search_dirblock(bh, dir, d_name,
  865. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  866. if (i == 1) {
  867. EXT4_I(dir)->i_dir_start_lookup = block;
  868. ret = bh;
  869. goto cleanup_and_exit;
  870. } else {
  871. brelse(bh);
  872. if (i < 0)
  873. goto cleanup_and_exit;
  874. }
  875. next:
  876. if (++block >= nblocks)
  877. block = 0;
  878. } while (block != start);
  879. /*
  880. * If the directory has grown while we were searching, then
  881. * search the last part of the directory before giving up.
  882. */
  883. block = nblocks;
  884. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  885. if (block < nblocks) {
  886. start = 0;
  887. goto restart;
  888. }
  889. cleanup_and_exit:
  890. /* Clean up the read-ahead blocks */
  891. for (; ra_ptr < ra_max; ra_ptr++)
  892. brelse(bh_use[ra_ptr]);
  893. return ret;
  894. }
  895. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  896. struct ext4_dir_entry_2 **res_dir, int *err)
  897. {
  898. struct super_block * sb = dir->i_sb;
  899. struct dx_hash_info hinfo;
  900. struct dx_frame frames[2], *frame;
  901. struct buffer_head *bh;
  902. ext4_lblk_t block;
  903. int retval;
  904. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  905. return NULL;
  906. do {
  907. block = dx_get_block(frame->at);
  908. if (!(bh = ext4_bread(NULL, dir, block, 0, err)))
  909. goto errout;
  910. retval = search_dirblock(bh, dir, d_name,
  911. block << EXT4_BLOCK_SIZE_BITS(sb),
  912. res_dir);
  913. if (retval == 1) { /* Success! */
  914. dx_release(frames);
  915. return bh;
  916. }
  917. brelse(bh);
  918. if (retval == -1) {
  919. *err = ERR_BAD_DX_DIR;
  920. goto errout;
  921. }
  922. /* Check to see if we should continue to search */
  923. retval = ext4_htree_next_block(dir, hinfo.hash, frame,
  924. frames, NULL);
  925. if (retval < 0) {
  926. ext4_warning(sb,
  927. "error reading index page in directory #%lu",
  928. dir->i_ino);
  929. *err = retval;
  930. goto errout;
  931. }
  932. } while (retval == 1);
  933. *err = -ENOENT;
  934. errout:
  935. dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
  936. dx_release (frames);
  937. return NULL;
  938. }
  939. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  940. {
  941. struct inode *inode;
  942. struct ext4_dir_entry_2 *de;
  943. struct buffer_head *bh;
  944. if (dentry->d_name.len > EXT4_NAME_LEN)
  945. return ERR_PTR(-ENAMETOOLONG);
  946. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  947. inode = NULL;
  948. if (bh) {
  949. __u32 ino = le32_to_cpu(de->inode);
  950. brelse(bh);
  951. if (!ext4_valid_inum(dir->i_sb, ino)) {
  952. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  953. return ERR_PTR(-EIO);
  954. }
  955. inode = ext4_iget(dir->i_sb, ino);
  956. if (inode == ERR_PTR(-ESTALE)) {
  957. EXT4_ERROR_INODE(dir,
  958. "deleted inode referenced: %u",
  959. ino);
  960. return ERR_PTR(-EIO);
  961. }
  962. }
  963. return d_splice_alias(inode, dentry);
  964. }
  965. struct dentry *ext4_get_parent(struct dentry *child)
  966. {
  967. __u32 ino;
  968. static const struct qstr dotdot = {
  969. .name = "..",
  970. .len = 2,
  971. };
  972. struct ext4_dir_entry_2 * de;
  973. struct buffer_head *bh;
  974. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  975. if (!bh)
  976. return ERR_PTR(-ENOENT);
  977. ino = le32_to_cpu(de->inode);
  978. brelse(bh);
  979. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  980. EXT4_ERROR_INODE(child->d_inode,
  981. "bad parent inode number: %u", ino);
  982. return ERR_PTR(-EIO);
  983. }
  984. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  985. }
  986. #define S_SHIFT 12
  987. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  988. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  989. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  990. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  991. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  992. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  993. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  994. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  995. };
  996. static inline void ext4_set_de_type(struct super_block *sb,
  997. struct ext4_dir_entry_2 *de,
  998. umode_t mode) {
  999. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1000. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1001. }
  1002. /*
  1003. * Move count entries from end of map between two memory locations.
  1004. * Returns pointer to last entry moved.
  1005. */
  1006. static struct ext4_dir_entry_2 *
  1007. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1008. unsigned blocksize)
  1009. {
  1010. unsigned rec_len = 0;
  1011. while (count--) {
  1012. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1013. (from + (map->offs<<2));
  1014. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1015. memcpy (to, de, rec_len);
  1016. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1017. ext4_rec_len_to_disk(rec_len, blocksize);
  1018. de->inode = 0;
  1019. map++;
  1020. to += rec_len;
  1021. }
  1022. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1023. }
  1024. /*
  1025. * Compact each dir entry in the range to the minimal rec_len.
  1026. * Returns pointer to last entry in range.
  1027. */
  1028. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1029. {
  1030. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1031. unsigned rec_len = 0;
  1032. prev = to = de;
  1033. while ((char*)de < base + blocksize) {
  1034. next = ext4_next_entry(de, blocksize);
  1035. if (de->inode && de->name_len) {
  1036. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1037. if (de > to)
  1038. memmove(to, de, rec_len);
  1039. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1040. prev = to;
  1041. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1042. }
  1043. de = next;
  1044. }
  1045. return prev;
  1046. }
  1047. /*
  1048. * Split a full leaf block to make room for a new dir entry.
  1049. * Allocate a new block, and move entries so that they are approx. equally full.
  1050. * Returns pointer to de in block into which the new entry will be inserted.
  1051. */
  1052. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1053. struct buffer_head **bh,struct dx_frame *frame,
  1054. struct dx_hash_info *hinfo, int *error)
  1055. {
  1056. unsigned blocksize = dir->i_sb->s_blocksize;
  1057. unsigned count, continued;
  1058. struct buffer_head *bh2;
  1059. ext4_lblk_t newblock;
  1060. u32 hash2;
  1061. struct dx_map_entry *map;
  1062. char *data1 = (*bh)->b_data, *data2;
  1063. unsigned split, move, size;
  1064. struct ext4_dir_entry_2 *de = NULL, *de2;
  1065. int err = 0, i;
  1066. bh2 = ext4_append (handle, dir, &newblock, &err);
  1067. if (!(bh2)) {
  1068. brelse(*bh);
  1069. *bh = NULL;
  1070. goto errout;
  1071. }
  1072. BUFFER_TRACE(*bh, "get_write_access");
  1073. err = ext4_journal_get_write_access(handle, *bh);
  1074. if (err)
  1075. goto journal_error;
  1076. BUFFER_TRACE(frame->bh, "get_write_access");
  1077. err = ext4_journal_get_write_access(handle, frame->bh);
  1078. if (err)
  1079. goto journal_error;
  1080. data2 = bh2->b_data;
  1081. /* create map in the end of data2 block */
  1082. map = (struct dx_map_entry *) (data2 + blocksize);
  1083. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1084. blocksize, hinfo, map);
  1085. map -= count;
  1086. dx_sort_map(map, count);
  1087. /* Split the existing block in the middle, size-wise */
  1088. size = 0;
  1089. move = 0;
  1090. for (i = count-1; i >= 0; i--) {
  1091. /* is more than half of this entry in 2nd half of the block? */
  1092. if (size + map[i].size/2 > blocksize/2)
  1093. break;
  1094. size += map[i].size;
  1095. move++;
  1096. }
  1097. /* map index at which we will split */
  1098. split = count - move;
  1099. hash2 = map[split].hash;
  1100. continued = hash2 == map[split - 1].hash;
  1101. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1102. (unsigned long)dx_get_block(frame->at),
  1103. hash2, split, count-split));
  1104. /* Fancy dance to stay within two buffers */
  1105. de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
  1106. de = dx_pack_dirents(data1, blocksize);
  1107. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1108. blocksize);
  1109. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2,
  1110. blocksize);
  1111. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1112. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1113. /* Which block gets the new entry? */
  1114. if (hinfo->hash >= hash2)
  1115. {
  1116. swap(*bh, bh2);
  1117. de = de2;
  1118. }
  1119. dx_insert_block(frame, hash2 + continued, newblock);
  1120. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1121. if (err)
  1122. goto journal_error;
  1123. err = ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1124. if (err)
  1125. goto journal_error;
  1126. brelse(bh2);
  1127. dxtrace(dx_show_index("frame", frame->entries));
  1128. return de;
  1129. journal_error:
  1130. brelse(*bh);
  1131. brelse(bh2);
  1132. *bh = NULL;
  1133. ext4_std_error(dir->i_sb, err);
  1134. errout:
  1135. *error = err;
  1136. return NULL;
  1137. }
  1138. /*
  1139. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1140. * it points to a directory entry which is guaranteed to be large
  1141. * enough for new directory entry. If de is NULL, then
  1142. * add_dirent_to_buf will attempt search the directory block for
  1143. * space. It will return -ENOSPC if no space is available, and -EIO
  1144. * and -EEXIST if directory entry already exists.
  1145. */
  1146. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1147. struct inode *inode, struct ext4_dir_entry_2 *de,
  1148. struct buffer_head *bh)
  1149. {
  1150. struct inode *dir = dentry->d_parent->d_inode;
  1151. const char *name = dentry->d_name.name;
  1152. int namelen = dentry->d_name.len;
  1153. unsigned int offset = 0;
  1154. unsigned int blocksize = dir->i_sb->s_blocksize;
  1155. unsigned short reclen;
  1156. int nlen, rlen, err;
  1157. char *top;
  1158. reclen = EXT4_DIR_REC_LEN(namelen);
  1159. if (!de) {
  1160. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1161. top = bh->b_data + blocksize - reclen;
  1162. while ((char *) de <= top) {
  1163. if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
  1164. return -EIO;
  1165. if (ext4_match(namelen, name, de))
  1166. return -EEXIST;
  1167. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1168. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1169. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1170. break;
  1171. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1172. offset += rlen;
  1173. }
  1174. if ((char *) de > top)
  1175. return -ENOSPC;
  1176. }
  1177. BUFFER_TRACE(bh, "get_write_access");
  1178. err = ext4_journal_get_write_access(handle, bh);
  1179. if (err) {
  1180. ext4_std_error(dir->i_sb, err);
  1181. return err;
  1182. }
  1183. /* By now the buffer is marked for journaling */
  1184. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1185. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1186. if (de->inode) {
  1187. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1188. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
  1189. de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
  1190. de = de1;
  1191. }
  1192. de->file_type = EXT4_FT_UNKNOWN;
  1193. if (inode) {
  1194. de->inode = cpu_to_le32(inode->i_ino);
  1195. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1196. } else
  1197. de->inode = 0;
  1198. de->name_len = namelen;
  1199. memcpy(de->name, name, namelen);
  1200. /*
  1201. * XXX shouldn't update any times until successful
  1202. * completion of syscall, but too many callers depend
  1203. * on this.
  1204. *
  1205. * XXX similarly, too many callers depend on
  1206. * ext4_new_inode() setting the times, but error
  1207. * recovery deletes the inode, so the worst that can
  1208. * happen is that the times are slightly out of date
  1209. * and/or different from the directory change time.
  1210. */
  1211. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1212. ext4_update_dx_flag(dir);
  1213. dir->i_version++;
  1214. ext4_mark_inode_dirty(handle, dir);
  1215. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1216. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1217. if (err)
  1218. ext4_std_error(dir->i_sb, err);
  1219. return 0;
  1220. }
  1221. /*
  1222. * This converts a one block unindexed directory to a 3 block indexed
  1223. * directory, and adds the dentry to the indexed directory.
  1224. */
  1225. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1226. struct inode *inode, struct buffer_head *bh)
  1227. {
  1228. struct inode *dir = dentry->d_parent->d_inode;
  1229. const char *name = dentry->d_name.name;
  1230. int namelen = dentry->d_name.len;
  1231. struct buffer_head *bh2;
  1232. struct dx_root *root;
  1233. struct dx_frame frames[2], *frame;
  1234. struct dx_entry *entries;
  1235. struct ext4_dir_entry_2 *de, *de2;
  1236. char *data1, *top;
  1237. unsigned len;
  1238. int retval;
  1239. unsigned blocksize;
  1240. struct dx_hash_info hinfo;
  1241. ext4_lblk_t block;
  1242. struct fake_dirent *fde;
  1243. blocksize = dir->i_sb->s_blocksize;
  1244. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1245. retval = ext4_journal_get_write_access(handle, bh);
  1246. if (retval) {
  1247. ext4_std_error(dir->i_sb, retval);
  1248. brelse(bh);
  1249. return retval;
  1250. }
  1251. root = (struct dx_root *) bh->b_data;
  1252. /* The 0th block becomes the root, move the dirents out */
  1253. fde = &root->dotdot;
  1254. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1255. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1256. if ((char *) de >= (((char *) root) + blocksize)) {
  1257. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1258. brelse(bh);
  1259. return -EIO;
  1260. }
  1261. len = ((char *) root) + blocksize - (char *) de;
  1262. /* Allocate new block for the 0th block's dirents */
  1263. bh2 = ext4_append(handle, dir, &block, &retval);
  1264. if (!(bh2)) {
  1265. brelse(bh);
  1266. return retval;
  1267. }
  1268. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1269. data1 = bh2->b_data;
  1270. memcpy (data1, de, len);
  1271. de = (struct ext4_dir_entry_2 *) data1;
  1272. top = data1 + len;
  1273. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1274. de = de2;
  1275. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1276. blocksize);
  1277. /* Initialize the root; the dot dirents already exist */
  1278. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1279. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1280. blocksize);
  1281. memset (&root->info, 0, sizeof(root->info));
  1282. root->info.info_length = sizeof(root->info);
  1283. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1284. entries = root->entries;
  1285. dx_set_block(entries, 1);
  1286. dx_set_count(entries, 1);
  1287. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1288. /* Initialize as for dx_probe */
  1289. hinfo.hash_version = root->info.hash_version;
  1290. if (hinfo.hash_version <= DX_HASH_TEA)
  1291. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1292. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1293. ext4fs_dirhash(name, namelen, &hinfo);
  1294. frame = frames;
  1295. frame->entries = entries;
  1296. frame->at = entries;
  1297. frame->bh = bh;
  1298. bh = bh2;
  1299. ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1300. ext4_handle_dirty_metadata(handle, dir, bh);
  1301. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1302. if (!de) {
  1303. /*
  1304. * Even if the block split failed, we have to properly write
  1305. * out all the changes we did so far. Otherwise we can end up
  1306. * with corrupted filesystem.
  1307. */
  1308. ext4_mark_inode_dirty(handle, dir);
  1309. dx_release(frames);
  1310. return retval;
  1311. }
  1312. dx_release(frames);
  1313. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1314. brelse(bh);
  1315. return retval;
  1316. }
  1317. /*
  1318. * ext4_add_entry()
  1319. *
  1320. * adds a file entry to the specified directory, using the same
  1321. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1322. *
  1323. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1324. * may not sleep between calling this and putting something into
  1325. * the entry, as someone else might have used it while you slept.
  1326. */
  1327. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1328. struct inode *inode)
  1329. {
  1330. struct inode *dir = dentry->d_parent->d_inode;
  1331. struct buffer_head *bh;
  1332. struct ext4_dir_entry_2 *de;
  1333. struct super_block *sb;
  1334. int retval;
  1335. int dx_fallback=0;
  1336. unsigned blocksize;
  1337. ext4_lblk_t block, blocks;
  1338. sb = dir->i_sb;
  1339. blocksize = sb->s_blocksize;
  1340. if (!dentry->d_name.len)
  1341. return -EINVAL;
  1342. if (is_dx(dir)) {
  1343. retval = ext4_dx_add_entry(handle, dentry, inode);
  1344. if (!retval || (retval != ERR_BAD_DX_DIR))
  1345. return retval;
  1346. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1347. dx_fallback++;
  1348. ext4_mark_inode_dirty(handle, dir);
  1349. }
  1350. blocks = dir->i_size >> sb->s_blocksize_bits;
  1351. for (block = 0; block < blocks; block++) {
  1352. bh = ext4_bread(handle, dir, block, 0, &retval);
  1353. if(!bh)
  1354. return retval;
  1355. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1356. if (retval != -ENOSPC) {
  1357. brelse(bh);
  1358. return retval;
  1359. }
  1360. if (blocks == 1 && !dx_fallback &&
  1361. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1362. return make_indexed_dir(handle, dentry, inode, bh);
  1363. brelse(bh);
  1364. }
  1365. bh = ext4_append(handle, dir, &block, &retval);
  1366. if (!bh)
  1367. return retval;
  1368. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1369. de->inode = 0;
  1370. de->rec_len = ext4_rec_len_to_disk(blocksize, blocksize);
  1371. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1372. brelse(bh);
  1373. if (retval == 0)
  1374. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1375. return retval;
  1376. }
  1377. /*
  1378. * Returns 0 for success, or a negative error value
  1379. */
  1380. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1381. struct inode *inode)
  1382. {
  1383. struct dx_frame frames[2], *frame;
  1384. struct dx_entry *entries, *at;
  1385. struct dx_hash_info hinfo;
  1386. struct buffer_head *bh;
  1387. struct inode *dir = dentry->d_parent->d_inode;
  1388. struct super_block *sb = dir->i_sb;
  1389. struct ext4_dir_entry_2 *de;
  1390. int err;
  1391. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1392. if (!frame)
  1393. return err;
  1394. entries = frame->entries;
  1395. at = frame->at;
  1396. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1397. goto cleanup;
  1398. BUFFER_TRACE(bh, "get_write_access");
  1399. err = ext4_journal_get_write_access(handle, bh);
  1400. if (err)
  1401. goto journal_error;
  1402. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1403. if (err != -ENOSPC)
  1404. goto cleanup;
  1405. /* Block full, should compress but for now just split */
  1406. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1407. dx_get_count(entries), dx_get_limit(entries)));
  1408. /* Need to split index? */
  1409. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1410. ext4_lblk_t newblock;
  1411. unsigned icount = dx_get_count(entries);
  1412. int levels = frame - frames;
  1413. struct dx_entry *entries2;
  1414. struct dx_node *node2;
  1415. struct buffer_head *bh2;
  1416. if (levels && (dx_get_count(frames->entries) ==
  1417. dx_get_limit(frames->entries))) {
  1418. ext4_warning(sb, "Directory index full!");
  1419. err = -ENOSPC;
  1420. goto cleanup;
  1421. }
  1422. bh2 = ext4_append (handle, dir, &newblock, &err);
  1423. if (!(bh2))
  1424. goto cleanup;
  1425. node2 = (struct dx_node *)(bh2->b_data);
  1426. entries2 = node2->entries;
  1427. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  1428. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  1429. sb->s_blocksize);
  1430. BUFFER_TRACE(frame->bh, "get_write_access");
  1431. err = ext4_journal_get_write_access(handle, frame->bh);
  1432. if (err)
  1433. goto journal_error;
  1434. if (levels) {
  1435. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1436. unsigned hash2 = dx_get_hash(entries + icount1);
  1437. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1438. icount1, icount2));
  1439. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1440. err = ext4_journal_get_write_access(handle,
  1441. frames[0].bh);
  1442. if (err)
  1443. goto journal_error;
  1444. memcpy((char *) entries2, (char *) (entries + icount1),
  1445. icount2 * sizeof(struct dx_entry));
  1446. dx_set_count(entries, icount1);
  1447. dx_set_count(entries2, icount2);
  1448. dx_set_limit(entries2, dx_node_limit(dir));
  1449. /* Which index block gets the new entry? */
  1450. if (at - entries >= icount1) {
  1451. frame->at = at = at - entries - icount1 + entries2;
  1452. frame->entries = entries = entries2;
  1453. swap(frame->bh, bh2);
  1454. }
  1455. dx_insert_block(frames + 0, hash2, newblock);
  1456. dxtrace(dx_show_index("node", frames[1].entries));
  1457. dxtrace(dx_show_index("node",
  1458. ((struct dx_node *) bh2->b_data)->entries));
  1459. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1460. if (err)
  1461. goto journal_error;
  1462. brelse (bh2);
  1463. } else {
  1464. dxtrace(printk(KERN_DEBUG
  1465. "Creating second level index...\n"));
  1466. memcpy((char *) entries2, (char *) entries,
  1467. icount * sizeof(struct dx_entry));
  1468. dx_set_limit(entries2, dx_node_limit(dir));
  1469. /* Set up root */
  1470. dx_set_count(entries, 1);
  1471. dx_set_block(entries + 0, newblock);
  1472. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1473. /* Add new access path frame */
  1474. frame = frames + 1;
  1475. frame->at = at = at - entries + entries2;
  1476. frame->entries = entries = entries2;
  1477. frame->bh = bh2;
  1478. err = ext4_journal_get_write_access(handle,
  1479. frame->bh);
  1480. if (err)
  1481. goto journal_error;
  1482. }
  1483. err = ext4_handle_dirty_metadata(handle, dir, frames[0].bh);
  1484. if (err) {
  1485. ext4_std_error(inode->i_sb, err);
  1486. goto cleanup;
  1487. }
  1488. }
  1489. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1490. if (!de)
  1491. goto cleanup;
  1492. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  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. unsigned int blocksize = dir->i_sb->s_blocksize;
  1513. int i, err;
  1514. i = 0;
  1515. pde = NULL;
  1516. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1517. while (i < bh->b_size) {
  1518. if (ext4_check_dir_entry(dir, NULL, de, bh, i))
  1519. return -EIO;
  1520. if (de == de_del) {
  1521. BUFFER_TRACE(bh, "get_write_access");
  1522. err = ext4_journal_get_write_access(handle, bh);
  1523. if (unlikely(err)) {
  1524. ext4_std_error(dir->i_sb, err);
  1525. return err;
  1526. }
  1527. if (pde)
  1528. pde->rec_len = ext4_rec_len_to_disk(
  1529. ext4_rec_len_from_disk(pde->rec_len,
  1530. blocksize) +
  1531. ext4_rec_len_from_disk(de->rec_len,
  1532. blocksize),
  1533. blocksize);
  1534. else
  1535. de->inode = 0;
  1536. dir->i_version++;
  1537. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1538. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1539. if (unlikely(err)) {
  1540. ext4_std_error(dir->i_sb, err);
  1541. return err;
  1542. }
  1543. return 0;
  1544. }
  1545. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  1546. pde = de;
  1547. de = ext4_next_entry(de, blocksize);
  1548. }
  1549. return -ENOENT;
  1550. }
  1551. /*
  1552. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1553. * since this indicates that nlinks count was previously 1.
  1554. */
  1555. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1556. {
  1557. inc_nlink(inode);
  1558. if (is_dx(inode) && inode->i_nlink > 1) {
  1559. /* limit is 16-bit i_links_count */
  1560. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1561. inode->i_nlink = 1;
  1562. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1563. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1564. }
  1565. }
  1566. }
  1567. /*
  1568. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1569. * directory has >EXT4_LINK_MAX subdirs.
  1570. */
  1571. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1572. {
  1573. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  1574. drop_nlink(inode);
  1575. }
  1576. static int ext4_add_nondir(handle_t *handle,
  1577. struct dentry *dentry, struct inode *inode)
  1578. {
  1579. int err = ext4_add_entry(handle, dentry, inode);
  1580. if (!err) {
  1581. ext4_mark_inode_dirty(handle, inode);
  1582. d_instantiate(dentry, inode);
  1583. unlock_new_inode(inode);
  1584. return 0;
  1585. }
  1586. drop_nlink(inode);
  1587. unlock_new_inode(inode);
  1588. iput(inode);
  1589. return err;
  1590. }
  1591. /*
  1592. * By the time this is called, we already have created
  1593. * the directory cache entry for the new file, but it
  1594. * is so far negative - it has no inode.
  1595. *
  1596. * If the create succeeds, we fill in the inode information
  1597. * with d_instantiate().
  1598. */
  1599. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1600. struct nameidata *nd)
  1601. {
  1602. handle_t *handle;
  1603. struct inode *inode;
  1604. int err, retries = 0;
  1605. dquot_initialize(dir);
  1606. retry:
  1607. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1608. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1609. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1610. if (IS_ERR(handle))
  1611. return PTR_ERR(handle);
  1612. if (IS_DIRSYNC(dir))
  1613. ext4_handle_sync(handle);
  1614. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
  1615. err = PTR_ERR(inode);
  1616. if (!IS_ERR(inode)) {
  1617. inode->i_op = &ext4_file_inode_operations;
  1618. inode->i_fop = &ext4_file_operations;
  1619. ext4_set_aops(inode);
  1620. err = ext4_add_nondir(handle, dentry, inode);
  1621. }
  1622. ext4_journal_stop(handle);
  1623. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1624. goto retry;
  1625. return err;
  1626. }
  1627. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1628. int mode, dev_t rdev)
  1629. {
  1630. handle_t *handle;
  1631. struct inode *inode;
  1632. int err, retries = 0;
  1633. if (!new_valid_dev(rdev))
  1634. return -EINVAL;
  1635. dquot_initialize(dir);
  1636. retry:
  1637. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1638. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1639. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1640. if (IS_ERR(handle))
  1641. return PTR_ERR(handle);
  1642. if (IS_DIRSYNC(dir))
  1643. ext4_handle_sync(handle);
  1644. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
  1645. err = PTR_ERR(inode);
  1646. if (!IS_ERR(inode)) {
  1647. init_special_inode(inode, inode->i_mode, rdev);
  1648. #ifdef CONFIG_EXT4_FS_XATTR
  1649. inode->i_op = &ext4_special_inode_operations;
  1650. #endif
  1651. err = ext4_add_nondir(handle, dentry, inode);
  1652. }
  1653. ext4_journal_stop(handle);
  1654. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1655. goto retry;
  1656. return err;
  1657. }
  1658. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1659. {
  1660. handle_t *handle;
  1661. struct inode *inode;
  1662. struct buffer_head *dir_block = NULL;
  1663. struct ext4_dir_entry_2 *de;
  1664. unsigned int blocksize = dir->i_sb->s_blocksize;
  1665. int err, retries = 0;
  1666. if (EXT4_DIR_LINK_MAX(dir))
  1667. return -EMLINK;
  1668. dquot_initialize(dir);
  1669. retry:
  1670. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1671. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1672. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1673. if (IS_ERR(handle))
  1674. return PTR_ERR(handle);
  1675. if (IS_DIRSYNC(dir))
  1676. ext4_handle_sync(handle);
  1677. inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
  1678. &dentry->d_name, 0, NULL);
  1679. err = PTR_ERR(inode);
  1680. if (IS_ERR(inode))
  1681. goto out_stop;
  1682. inode->i_op = &ext4_dir_inode_operations;
  1683. inode->i_fop = &ext4_dir_operations;
  1684. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1685. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1686. if (!dir_block)
  1687. goto out_clear_inode;
  1688. BUFFER_TRACE(dir_block, "get_write_access");
  1689. err = ext4_journal_get_write_access(handle, dir_block);
  1690. if (err)
  1691. goto out_clear_inode;
  1692. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1693. de->inode = cpu_to_le32(inode->i_ino);
  1694. de->name_len = 1;
  1695. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  1696. blocksize);
  1697. strcpy(de->name, ".");
  1698. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1699. de = ext4_next_entry(de, blocksize);
  1700. de->inode = cpu_to_le32(dir->i_ino);
  1701. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(1),
  1702. blocksize);
  1703. de->name_len = 2;
  1704. strcpy(de->name, "..");
  1705. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1706. inode->i_nlink = 2;
  1707. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  1708. err = ext4_handle_dirty_metadata(handle, inode, dir_block);
  1709. if (err)
  1710. goto out_clear_inode;
  1711. err = ext4_mark_inode_dirty(handle, inode);
  1712. if (!err)
  1713. err = ext4_add_entry(handle, dentry, inode);
  1714. if (err) {
  1715. out_clear_inode:
  1716. clear_nlink(inode);
  1717. unlock_new_inode(inode);
  1718. ext4_mark_inode_dirty(handle, inode);
  1719. iput(inode);
  1720. goto out_stop;
  1721. }
  1722. ext4_inc_count(handle, dir);
  1723. ext4_update_dx_flag(dir);
  1724. err = ext4_mark_inode_dirty(handle, dir);
  1725. if (err)
  1726. goto out_clear_inode;
  1727. d_instantiate(dentry, inode);
  1728. unlock_new_inode(inode);
  1729. out_stop:
  1730. brelse(dir_block);
  1731. ext4_journal_stop(handle);
  1732. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1733. goto retry;
  1734. return err;
  1735. }
  1736. /*
  1737. * routine to check that the specified directory is empty (for rmdir)
  1738. */
  1739. static int empty_dir(struct inode *inode)
  1740. {
  1741. unsigned int offset;
  1742. struct buffer_head *bh;
  1743. struct ext4_dir_entry_2 *de, *de1;
  1744. struct super_block *sb;
  1745. int err = 0;
  1746. sb = inode->i_sb;
  1747. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1748. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1749. if (err)
  1750. EXT4_ERROR_INODE(inode,
  1751. "error %d reading directory lblock 0", err);
  1752. else
  1753. ext4_warning(inode->i_sb,
  1754. "bad directory (dir #%lu) - no data block",
  1755. inode->i_ino);
  1756. return 1;
  1757. }
  1758. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1759. de1 = ext4_next_entry(de, sb->s_blocksize);
  1760. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1761. !le32_to_cpu(de1->inode) ||
  1762. strcmp(".", de->name) ||
  1763. strcmp("..", de1->name)) {
  1764. ext4_warning(inode->i_sb,
  1765. "bad directory (dir #%lu) - no `.' or `..'",
  1766. inode->i_ino);
  1767. brelse(bh);
  1768. return 1;
  1769. }
  1770. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  1771. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  1772. de = ext4_next_entry(de1, sb->s_blocksize);
  1773. while (offset < inode->i_size) {
  1774. if (!bh ||
  1775. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1776. unsigned int lblock;
  1777. err = 0;
  1778. brelse(bh);
  1779. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  1780. bh = ext4_bread(NULL, inode, lblock, 0, &err);
  1781. if (!bh) {
  1782. if (err)
  1783. EXT4_ERROR_INODE(inode,
  1784. "error %d reading directory "
  1785. "lblock %u", err, lblock);
  1786. offset += sb->s_blocksize;
  1787. continue;
  1788. }
  1789. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1790. }
  1791. if (ext4_check_dir_entry(inode, NULL, de, bh, offset)) {
  1792. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1793. sb->s_blocksize);
  1794. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1795. continue;
  1796. }
  1797. if (le32_to_cpu(de->inode)) {
  1798. brelse(bh);
  1799. return 0;
  1800. }
  1801. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  1802. de = ext4_next_entry(de, sb->s_blocksize);
  1803. }
  1804. brelse(bh);
  1805. return 1;
  1806. }
  1807. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1808. * such inodes, starting at the superblock, in case we crash before the
  1809. * file is closed/deleted, or in case the inode truncate spans multiple
  1810. * transactions and the last transaction is not recovered after a crash.
  1811. *
  1812. * At filesystem recovery time, we walk this list deleting unlinked
  1813. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1814. */
  1815. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1816. {
  1817. struct super_block *sb = inode->i_sb;
  1818. struct ext4_iloc iloc;
  1819. int err = 0, rc;
  1820. if (!ext4_handle_valid(handle))
  1821. return 0;
  1822. mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
  1823. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1824. goto out_unlock;
  1825. /*
  1826. * Orphan handling is only valid for files with data blocks
  1827. * being truncated, or files being unlinked. Note that we either
  1828. * hold i_mutex, or the inode can not be referenced from outside,
  1829. * so i_nlink should not be bumped due to race
  1830. */
  1831. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1832. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1833. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1834. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1835. if (err)
  1836. goto out_unlock;
  1837. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1838. if (err)
  1839. goto out_unlock;
  1840. /*
  1841. * Due to previous errors inode may be already a part of on-disk
  1842. * orphan list. If so skip on-disk list modification.
  1843. */
  1844. if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
  1845. (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
  1846. goto mem_insert;
  1847. /* Insert this inode at the head of the on-disk orphan list... */
  1848. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1849. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1850. err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
  1851. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1852. if (!err)
  1853. err = rc;
  1854. /* Only add to the head of the in-memory list if all the
  1855. * previous operations succeeded. If the orphan_add is going to
  1856. * fail (possibly taking the journal offline), we can't risk
  1857. * leaving the inode on the orphan list: stray orphan-list
  1858. * entries can cause panics at unmount time.
  1859. *
  1860. * This is safe: on error we're going to ignore the orphan list
  1861. * anyway on the next recovery. */
  1862. mem_insert:
  1863. if (!err)
  1864. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1865. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1866. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1867. inode->i_ino, NEXT_ORPHAN(inode));
  1868. out_unlock:
  1869. mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
  1870. ext4_std_error(inode->i_sb, err);
  1871. return err;
  1872. }
  1873. /*
  1874. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1875. * of such inodes stored on disk, because it is finally being cleaned up.
  1876. */
  1877. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1878. {
  1879. struct list_head *prev;
  1880. struct ext4_inode_info *ei = EXT4_I(inode);
  1881. struct ext4_sb_info *sbi;
  1882. __u32 ino_next;
  1883. struct ext4_iloc iloc;
  1884. int err = 0;
  1885. /* ext4_handle_valid() assumes a valid handle_t pointer */
  1886. if (handle && !ext4_handle_valid(handle))
  1887. return 0;
  1888. mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  1889. if (list_empty(&ei->i_orphan))
  1890. goto out;
  1891. ino_next = NEXT_ORPHAN(inode);
  1892. prev = ei->i_orphan.prev;
  1893. sbi = EXT4_SB(inode->i_sb);
  1894. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1895. list_del_init(&ei->i_orphan);
  1896. /* If we're on an error path, we may not have a valid
  1897. * transaction handle with which to update the orphan list on
  1898. * disk, but we still need to remove the inode from the linked
  1899. * list in memory. */
  1900. if (sbi->s_journal && !handle)
  1901. goto out;
  1902. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1903. if (err)
  1904. goto out_err;
  1905. if (prev == &sbi->s_orphan) {
  1906. jbd_debug(4, "superblock will point to %u\n", ino_next);
  1907. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1908. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1909. if (err)
  1910. goto out_brelse;
  1911. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1912. err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
  1913. } else {
  1914. struct ext4_iloc iloc2;
  1915. struct inode *i_prev =
  1916. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1917. jbd_debug(4, "orphan inode %lu will point to %u\n",
  1918. i_prev->i_ino, ino_next);
  1919. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1920. if (err)
  1921. goto out_brelse;
  1922. NEXT_ORPHAN(i_prev) = ino_next;
  1923. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1924. }
  1925. if (err)
  1926. goto out_brelse;
  1927. NEXT_ORPHAN(inode) = 0;
  1928. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1929. out_err:
  1930. ext4_std_error(inode->i_sb, err);
  1931. out:
  1932. mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  1933. return err;
  1934. out_brelse:
  1935. brelse(iloc.bh);
  1936. goto out_err;
  1937. }
  1938. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  1939. {
  1940. int retval;
  1941. struct inode *inode;
  1942. struct buffer_head *bh;
  1943. struct ext4_dir_entry_2 *de;
  1944. handle_t *handle;
  1945. /* Initialize quotas before so that eventual writes go in
  1946. * separate transaction */
  1947. dquot_initialize(dir);
  1948. dquot_initialize(dentry->d_inode);
  1949. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1950. if (IS_ERR(handle))
  1951. return PTR_ERR(handle);
  1952. retval = -ENOENT;
  1953. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1954. if (!bh)
  1955. goto end_rmdir;
  1956. if (IS_DIRSYNC(dir))
  1957. ext4_handle_sync(handle);
  1958. inode = dentry->d_inode;
  1959. retval = -EIO;
  1960. if (le32_to_cpu(de->inode) != inode->i_ino)
  1961. goto end_rmdir;
  1962. retval = -ENOTEMPTY;
  1963. if (!empty_dir(inode))
  1964. goto end_rmdir;
  1965. retval = ext4_delete_entry(handle, dir, de, bh);
  1966. if (retval)
  1967. goto end_rmdir;
  1968. if (!EXT4_DIR_LINK_EMPTY(inode))
  1969. ext4_warning(inode->i_sb,
  1970. "empty directory has too many links (%d)",
  1971. inode->i_nlink);
  1972. inode->i_version++;
  1973. clear_nlink(inode);
  1974. /* There's no need to set i_disksize: the fact that i_nlink is
  1975. * zero will ensure that the right thing happens during any
  1976. * recovery. */
  1977. inode->i_size = 0;
  1978. ext4_orphan_add(handle, inode);
  1979. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1980. ext4_mark_inode_dirty(handle, inode);
  1981. ext4_dec_count(handle, dir);
  1982. ext4_update_dx_flag(dir);
  1983. ext4_mark_inode_dirty(handle, dir);
  1984. end_rmdir:
  1985. ext4_journal_stop(handle);
  1986. brelse(bh);
  1987. return retval;
  1988. }
  1989. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  1990. {
  1991. int retval;
  1992. struct inode *inode;
  1993. struct buffer_head *bh;
  1994. struct ext4_dir_entry_2 *de;
  1995. handle_t *handle;
  1996. trace_ext4_unlink_enter(dir, dentry);
  1997. /* Initialize quotas before so that eventual writes go
  1998. * in separate transaction */
  1999. dquot_initialize(dir);
  2000. dquot_initialize(dentry->d_inode);
  2001. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  2002. if (IS_ERR(handle))
  2003. return PTR_ERR(handle);
  2004. if (IS_DIRSYNC(dir))
  2005. ext4_handle_sync(handle);
  2006. retval = -ENOENT;
  2007. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  2008. if (!bh)
  2009. goto end_unlink;
  2010. inode = dentry->d_inode;
  2011. retval = -EIO;
  2012. if (le32_to_cpu(de->inode) != inode->i_ino)
  2013. goto end_unlink;
  2014. if (!inode->i_nlink) {
  2015. ext4_warning(inode->i_sb,
  2016. "Deleting nonexistent file (%lu), %d",
  2017. inode->i_ino, inode->i_nlink);
  2018. inode->i_nlink = 1;
  2019. }
  2020. retval = ext4_delete_entry(handle, dir, de, bh);
  2021. if (retval)
  2022. goto end_unlink;
  2023. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  2024. ext4_update_dx_flag(dir);
  2025. ext4_mark_inode_dirty(handle, dir);
  2026. drop_nlink(inode);
  2027. if (!inode->i_nlink)
  2028. ext4_orphan_add(handle, inode);
  2029. inode->i_ctime = ext4_current_time(inode);
  2030. ext4_mark_inode_dirty(handle, inode);
  2031. retval = 0;
  2032. end_unlink:
  2033. ext4_journal_stop(handle);
  2034. brelse(bh);
  2035. trace_ext4_unlink_exit(dentry, retval);
  2036. return retval;
  2037. }
  2038. static int ext4_symlink(struct inode *dir,
  2039. struct dentry *dentry, const char *symname)
  2040. {
  2041. handle_t *handle;
  2042. struct inode *inode;
  2043. int l, err, retries = 0;
  2044. int credits;
  2045. l = strlen(symname)+1;
  2046. if (l > dir->i_sb->s_blocksize)
  2047. return -ENAMETOOLONG;
  2048. dquot_initialize(dir);
  2049. if (l > EXT4_N_BLOCKS * 4) {
  2050. /*
  2051. * For non-fast symlinks, we just allocate inode and put it on
  2052. * orphan list in the first transaction => we need bitmap,
  2053. * group descriptor, sb, inode block, quota blocks, and
  2054. * possibly selinux xattr blocks.
  2055. */
  2056. credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2057. EXT4_XATTR_TRANS_BLOCKS;
  2058. } else {
  2059. /*
  2060. * Fast symlink. We have to add entry to directory
  2061. * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
  2062. * allocate new inode (bitmap, group descriptor, inode block,
  2063. * quota blocks, sb is already counted in previous macros).
  2064. */
  2065. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2066. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  2067. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
  2068. }
  2069. retry:
  2070. handle = ext4_journal_start(dir, credits);
  2071. if (IS_ERR(handle))
  2072. return PTR_ERR(handle);
  2073. if (IS_DIRSYNC(dir))
  2074. ext4_handle_sync(handle);
  2075. inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
  2076. &dentry->d_name, 0, NULL);
  2077. err = PTR_ERR(inode);
  2078. if (IS_ERR(inode))
  2079. goto out_stop;
  2080. if (l > EXT4_N_BLOCKS * 4) {
  2081. inode->i_op = &ext4_symlink_inode_operations;
  2082. ext4_set_aops(inode);
  2083. /*
  2084. * We cannot call page_symlink() with transaction started
  2085. * because it calls into ext4_write_begin() which can wait
  2086. * for transaction commit if we are running out of space
  2087. * and thus we deadlock. So we have to stop transaction now
  2088. * and restart it when symlink contents is written.
  2089. *
  2090. * To keep fs consistent in case of crash, we have to put inode
  2091. * to orphan list in the mean time.
  2092. */
  2093. drop_nlink(inode);
  2094. err = ext4_orphan_add(handle, inode);
  2095. ext4_journal_stop(handle);
  2096. if (err)
  2097. goto err_drop_inode;
  2098. err = __page_symlink(inode, symname, l, 1);
  2099. if (err)
  2100. goto err_drop_inode;
  2101. /*
  2102. * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
  2103. * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
  2104. */
  2105. handle = ext4_journal_start(dir,
  2106. EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2107. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
  2108. if (IS_ERR(handle)) {
  2109. err = PTR_ERR(handle);
  2110. goto err_drop_inode;
  2111. }
  2112. inc_nlink(inode);
  2113. err = ext4_orphan_del(handle, inode);
  2114. if (err) {
  2115. ext4_journal_stop(handle);
  2116. clear_nlink(inode);
  2117. goto err_drop_inode;
  2118. }
  2119. } else {
  2120. /* clear the extent format for fast symlink */
  2121. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2122. inode->i_op = &ext4_fast_symlink_inode_operations;
  2123. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2124. inode->i_size = l-1;
  2125. }
  2126. EXT4_I(inode)->i_disksize = inode->i_size;
  2127. err = ext4_add_nondir(handle, dentry, inode);
  2128. out_stop:
  2129. ext4_journal_stop(handle);
  2130. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2131. goto retry;
  2132. return err;
  2133. err_drop_inode:
  2134. unlock_new_inode(inode);
  2135. iput(inode);
  2136. return err;
  2137. }
  2138. static int ext4_link(struct dentry *old_dentry,
  2139. struct inode *dir, struct dentry *dentry)
  2140. {
  2141. handle_t *handle;
  2142. struct inode *inode = old_dentry->d_inode;
  2143. int err, retries = 0;
  2144. if (inode->i_nlink >= EXT4_LINK_MAX)
  2145. return -EMLINK;
  2146. dquot_initialize(dir);
  2147. retry:
  2148. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2149. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2150. if (IS_ERR(handle))
  2151. return PTR_ERR(handle);
  2152. if (IS_DIRSYNC(dir))
  2153. ext4_handle_sync(handle);
  2154. inode->i_ctime = ext4_current_time(inode);
  2155. ext4_inc_count(handle, inode);
  2156. ihold(inode);
  2157. err = ext4_add_entry(handle, dentry, inode);
  2158. if (!err) {
  2159. ext4_mark_inode_dirty(handle, inode);
  2160. d_instantiate(dentry, inode);
  2161. } else {
  2162. drop_nlink(inode);
  2163. iput(inode);
  2164. }
  2165. ext4_journal_stop(handle);
  2166. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2167. goto retry;
  2168. return err;
  2169. }
  2170. #define PARENT_INO(buffer, size) \
  2171. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
  2172. /*
  2173. * Anybody can rename anything with this: the permission checks are left to the
  2174. * higher-level routines.
  2175. */
  2176. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2177. struct inode *new_dir, struct dentry *new_dentry)
  2178. {
  2179. handle_t *handle;
  2180. struct inode *old_inode, *new_inode;
  2181. struct buffer_head *old_bh, *new_bh, *dir_bh;
  2182. struct ext4_dir_entry_2 *old_de, *new_de;
  2183. int retval, force_da_alloc = 0;
  2184. dquot_initialize(old_dir);
  2185. dquot_initialize(new_dir);
  2186. old_bh = new_bh = dir_bh = NULL;
  2187. /* Initialize quotas before so that eventual writes go
  2188. * in separate transaction */
  2189. if (new_dentry->d_inode)
  2190. dquot_initialize(new_dentry->d_inode);
  2191. handle = ext4_journal_start(old_dir, 2 *
  2192. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2193. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2194. if (IS_ERR(handle))
  2195. return PTR_ERR(handle);
  2196. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2197. ext4_handle_sync(handle);
  2198. old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
  2199. /*
  2200. * Check for inode number is _not_ due to possible IO errors.
  2201. * We might rmdir the source, keep it as pwd of some process
  2202. * and merrily kill the link to whatever was created under the
  2203. * same name. Goodbye sticky bit ;-<
  2204. */
  2205. old_inode = old_dentry->d_inode;
  2206. retval = -ENOENT;
  2207. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2208. goto end_rename;
  2209. new_inode = new_dentry->d_inode;
  2210. new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
  2211. if (new_bh) {
  2212. if (!new_inode) {
  2213. brelse(new_bh);
  2214. new_bh = NULL;
  2215. }
  2216. }
  2217. if (S_ISDIR(old_inode->i_mode)) {
  2218. if (new_inode) {
  2219. retval = -ENOTEMPTY;
  2220. if (!empty_dir(new_inode))
  2221. goto end_rename;
  2222. }
  2223. retval = -EIO;
  2224. dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
  2225. if (!dir_bh)
  2226. goto end_rename;
  2227. if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
  2228. old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
  2229. goto end_rename;
  2230. retval = -EMLINK;
  2231. if (!new_inode && new_dir != old_dir &&
  2232. EXT4_DIR_LINK_MAX(new_dir))
  2233. goto end_rename;
  2234. BUFFER_TRACE(dir_bh, "get_write_access");
  2235. retval = ext4_journal_get_write_access(handle, dir_bh);
  2236. if (retval)
  2237. goto end_rename;
  2238. }
  2239. if (!new_bh) {
  2240. retval = ext4_add_entry(handle, new_dentry, old_inode);
  2241. if (retval)
  2242. goto end_rename;
  2243. } else {
  2244. BUFFER_TRACE(new_bh, "get write access");
  2245. retval = ext4_journal_get_write_access(handle, new_bh);
  2246. if (retval)
  2247. goto end_rename;
  2248. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2249. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2250. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2251. new_de->file_type = old_de->file_type;
  2252. new_dir->i_version++;
  2253. new_dir->i_ctime = new_dir->i_mtime =
  2254. ext4_current_time(new_dir);
  2255. ext4_mark_inode_dirty(handle, new_dir);
  2256. BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
  2257. retval = ext4_handle_dirty_metadata(handle, new_dir, new_bh);
  2258. if (unlikely(retval)) {
  2259. ext4_std_error(new_dir->i_sb, retval);
  2260. goto end_rename;
  2261. }
  2262. brelse(new_bh);
  2263. new_bh = NULL;
  2264. }
  2265. /*
  2266. * Like most other Unix systems, set the ctime for inodes on a
  2267. * rename.
  2268. */
  2269. old_inode->i_ctime = ext4_current_time(old_inode);
  2270. ext4_mark_inode_dirty(handle, old_inode);
  2271. /*
  2272. * ok, that's it
  2273. */
  2274. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2275. old_de->name_len != old_dentry->d_name.len ||
  2276. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2277. (retval = ext4_delete_entry(handle, old_dir,
  2278. old_de, old_bh)) == -ENOENT) {
  2279. /* old_de could have moved from under us during htree split, so
  2280. * make sure that we are deleting the right entry. We might
  2281. * also be pointing to a stale entry in the unused part of
  2282. * old_bh so just checking inum and the name isn't enough. */
  2283. struct buffer_head *old_bh2;
  2284. struct ext4_dir_entry_2 *old_de2;
  2285. old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
  2286. if (old_bh2) {
  2287. retval = ext4_delete_entry(handle, old_dir,
  2288. old_de2, old_bh2);
  2289. brelse(old_bh2);
  2290. }
  2291. }
  2292. if (retval) {
  2293. ext4_warning(old_dir->i_sb,
  2294. "Deleting old file (%lu), %d, error=%d",
  2295. old_dir->i_ino, old_dir->i_nlink, retval);
  2296. }
  2297. if (new_inode) {
  2298. ext4_dec_count(handle, new_inode);
  2299. new_inode->i_ctime = ext4_current_time(new_inode);
  2300. }
  2301. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2302. ext4_update_dx_flag(old_dir);
  2303. if (dir_bh) {
  2304. PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
  2305. cpu_to_le32(new_dir->i_ino);
  2306. BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
  2307. retval = ext4_handle_dirty_metadata(handle, old_inode, dir_bh);
  2308. if (retval) {
  2309. ext4_std_error(old_dir->i_sb, retval);
  2310. goto end_rename;
  2311. }
  2312. ext4_dec_count(handle, old_dir);
  2313. if (new_inode) {
  2314. /* checked empty_dir above, can't have another parent,
  2315. * ext4_dec_count() won't work for many-linked dirs */
  2316. new_inode->i_nlink = 0;
  2317. } else {
  2318. ext4_inc_count(handle, new_dir);
  2319. ext4_update_dx_flag(new_dir);
  2320. ext4_mark_inode_dirty(handle, new_dir);
  2321. }
  2322. }
  2323. ext4_mark_inode_dirty(handle, old_dir);
  2324. if (new_inode) {
  2325. ext4_mark_inode_dirty(handle, new_inode);
  2326. if (!new_inode->i_nlink)
  2327. ext4_orphan_add(handle, new_inode);
  2328. if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
  2329. force_da_alloc = 1;
  2330. }
  2331. retval = 0;
  2332. end_rename:
  2333. brelse(dir_bh);
  2334. brelse(old_bh);
  2335. brelse(new_bh);
  2336. ext4_journal_stop(handle);
  2337. if (retval == 0 && force_da_alloc)
  2338. ext4_alloc_da_blocks(old_inode);
  2339. return retval;
  2340. }
  2341. /*
  2342. * directories can handle most operations...
  2343. */
  2344. const struct inode_operations ext4_dir_inode_operations = {
  2345. .create = ext4_create,
  2346. .lookup = ext4_lookup,
  2347. .link = ext4_link,
  2348. .unlink = ext4_unlink,
  2349. .symlink = ext4_symlink,
  2350. .mkdir = ext4_mkdir,
  2351. .rmdir = ext4_rmdir,
  2352. .mknod = ext4_mknod,
  2353. .rename = ext4_rename,
  2354. .setattr = ext4_setattr,
  2355. #ifdef CONFIG_EXT4_FS_XATTR
  2356. .setxattr = generic_setxattr,
  2357. .getxattr = generic_getxattr,
  2358. .listxattr = ext4_listxattr,
  2359. .removexattr = generic_removexattr,
  2360. #endif
  2361. .get_acl = ext4_get_acl,
  2362. .fiemap = ext4_fiemap,
  2363. };
  2364. const struct inode_operations ext4_special_inode_operations = {
  2365. .setattr = ext4_setattr,
  2366. #ifdef CONFIG_EXT4_FS_XATTR
  2367. .setxattr = generic_setxattr,
  2368. .getxattr = generic_getxattr,
  2369. .listxattr = ext4_listxattr,
  2370. .removexattr = generic_removexattr,
  2371. #endif
  2372. .get_acl = ext4_get_acl,
  2373. };