namei.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  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. ((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", 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 (IS_ERR(inode)) {
  957. if (PTR_ERR(inode) == -ESTALE) {
  958. EXT4_ERROR_INODE(dir,
  959. "deleted inode referenced: %u",
  960. ino);
  961. return ERR_PTR(-EIO);
  962. } else {
  963. return ERR_CAST(inode);
  964. }
  965. }
  966. }
  967. return d_splice_alias(inode, dentry);
  968. }
  969. struct dentry *ext4_get_parent(struct dentry *child)
  970. {
  971. __u32 ino;
  972. static const struct qstr dotdot = {
  973. .name = "..",
  974. .len = 2,
  975. };
  976. struct ext4_dir_entry_2 * de;
  977. struct buffer_head *bh;
  978. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  979. if (!bh)
  980. return ERR_PTR(-ENOENT);
  981. ino = le32_to_cpu(de->inode);
  982. brelse(bh);
  983. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  984. EXT4_ERROR_INODE(child->d_inode,
  985. "bad parent inode number: %u", ino);
  986. return ERR_PTR(-EIO);
  987. }
  988. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  989. }
  990. #define S_SHIFT 12
  991. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  992. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  993. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  994. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  995. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  996. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  997. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  998. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  999. };
  1000. static inline void ext4_set_de_type(struct super_block *sb,
  1001. struct ext4_dir_entry_2 *de,
  1002. umode_t mode) {
  1003. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1004. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1005. }
  1006. /*
  1007. * Move count entries from end of map between two memory locations.
  1008. * Returns pointer to last entry moved.
  1009. */
  1010. static struct ext4_dir_entry_2 *
  1011. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1012. unsigned blocksize)
  1013. {
  1014. unsigned rec_len = 0;
  1015. while (count--) {
  1016. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1017. (from + (map->offs<<2));
  1018. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1019. memcpy (to, de, rec_len);
  1020. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1021. ext4_rec_len_to_disk(rec_len, blocksize);
  1022. de->inode = 0;
  1023. map++;
  1024. to += rec_len;
  1025. }
  1026. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1027. }
  1028. /*
  1029. * Compact each dir entry in the range to the minimal rec_len.
  1030. * Returns pointer to last entry in range.
  1031. */
  1032. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1033. {
  1034. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1035. unsigned rec_len = 0;
  1036. prev = to = de;
  1037. while ((char*)de < base + blocksize) {
  1038. next = ext4_next_entry(de, blocksize);
  1039. if (de->inode && de->name_len) {
  1040. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1041. if (de > to)
  1042. memmove(to, de, rec_len);
  1043. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1044. prev = to;
  1045. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1046. }
  1047. de = next;
  1048. }
  1049. return prev;
  1050. }
  1051. /*
  1052. * Split a full leaf block to make room for a new dir entry.
  1053. * Allocate a new block, and move entries so that they are approx. equally full.
  1054. * Returns pointer to de in block into which the new entry will be inserted.
  1055. */
  1056. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1057. struct buffer_head **bh,struct dx_frame *frame,
  1058. struct dx_hash_info *hinfo, int *error)
  1059. {
  1060. unsigned blocksize = dir->i_sb->s_blocksize;
  1061. unsigned count, continued;
  1062. struct buffer_head *bh2;
  1063. ext4_lblk_t newblock;
  1064. u32 hash2;
  1065. struct dx_map_entry *map;
  1066. char *data1 = (*bh)->b_data, *data2;
  1067. unsigned split, move, size;
  1068. struct ext4_dir_entry_2 *de = NULL, *de2;
  1069. int err = 0, i;
  1070. bh2 = ext4_append (handle, dir, &newblock, &err);
  1071. if (!(bh2)) {
  1072. brelse(*bh);
  1073. *bh = NULL;
  1074. goto errout;
  1075. }
  1076. BUFFER_TRACE(*bh, "get_write_access");
  1077. err = ext4_journal_get_write_access(handle, *bh);
  1078. if (err)
  1079. goto journal_error;
  1080. BUFFER_TRACE(frame->bh, "get_write_access");
  1081. err = ext4_journal_get_write_access(handle, frame->bh);
  1082. if (err)
  1083. goto journal_error;
  1084. data2 = bh2->b_data;
  1085. /* create map in the end of data2 block */
  1086. map = (struct dx_map_entry *) (data2 + blocksize);
  1087. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1088. blocksize, hinfo, map);
  1089. map -= count;
  1090. dx_sort_map(map, count);
  1091. /* Split the existing block in the middle, size-wise */
  1092. size = 0;
  1093. move = 0;
  1094. for (i = count-1; i >= 0; i--) {
  1095. /* is more than half of this entry in 2nd half of the block? */
  1096. if (size + map[i].size/2 > blocksize/2)
  1097. break;
  1098. size += map[i].size;
  1099. move++;
  1100. }
  1101. /* map index at which we will split */
  1102. split = count - move;
  1103. hash2 = map[split].hash;
  1104. continued = hash2 == map[split - 1].hash;
  1105. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1106. (unsigned long)dx_get_block(frame->at),
  1107. hash2, split, count-split));
  1108. /* Fancy dance to stay within two buffers */
  1109. de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
  1110. de = dx_pack_dirents(data1, blocksize);
  1111. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1112. blocksize);
  1113. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2,
  1114. blocksize);
  1115. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1116. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1117. /* Which block gets the new entry? */
  1118. if (hinfo->hash >= hash2)
  1119. {
  1120. swap(*bh, bh2);
  1121. de = de2;
  1122. }
  1123. dx_insert_block(frame, hash2 + continued, newblock);
  1124. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1125. if (err)
  1126. goto journal_error;
  1127. err = ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1128. if (err)
  1129. goto journal_error;
  1130. brelse(bh2);
  1131. dxtrace(dx_show_index("frame", frame->entries));
  1132. return de;
  1133. journal_error:
  1134. brelse(*bh);
  1135. brelse(bh2);
  1136. *bh = NULL;
  1137. ext4_std_error(dir->i_sb, err);
  1138. errout:
  1139. *error = err;
  1140. return NULL;
  1141. }
  1142. /*
  1143. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1144. * it points to a directory entry which is guaranteed to be large
  1145. * enough for new directory entry. If de is NULL, then
  1146. * add_dirent_to_buf will attempt search the directory block for
  1147. * space. It will return -ENOSPC if no space is available, and -EIO
  1148. * and -EEXIST if directory entry already exists.
  1149. */
  1150. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1151. struct inode *inode, struct ext4_dir_entry_2 *de,
  1152. struct buffer_head *bh)
  1153. {
  1154. struct inode *dir = dentry->d_parent->d_inode;
  1155. const char *name = dentry->d_name.name;
  1156. int namelen = dentry->d_name.len;
  1157. unsigned int offset = 0;
  1158. unsigned int blocksize = dir->i_sb->s_blocksize;
  1159. unsigned short reclen;
  1160. int nlen, rlen, err;
  1161. char *top;
  1162. reclen = EXT4_DIR_REC_LEN(namelen);
  1163. if (!de) {
  1164. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1165. top = bh->b_data + blocksize - reclen;
  1166. while ((char *) de <= top) {
  1167. if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
  1168. return -EIO;
  1169. if (ext4_match(namelen, name, de))
  1170. return -EEXIST;
  1171. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1172. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1173. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1174. break;
  1175. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1176. offset += rlen;
  1177. }
  1178. if ((char *) de > top)
  1179. return -ENOSPC;
  1180. }
  1181. BUFFER_TRACE(bh, "get_write_access");
  1182. err = ext4_journal_get_write_access(handle, bh);
  1183. if (err) {
  1184. ext4_std_error(dir->i_sb, err);
  1185. return err;
  1186. }
  1187. /* By now the buffer is marked for journaling */
  1188. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1189. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1190. if (de->inode) {
  1191. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1192. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
  1193. de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
  1194. de = de1;
  1195. }
  1196. de->file_type = EXT4_FT_UNKNOWN;
  1197. if (inode) {
  1198. de->inode = cpu_to_le32(inode->i_ino);
  1199. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1200. } else
  1201. de->inode = 0;
  1202. de->name_len = namelen;
  1203. memcpy(de->name, name, namelen);
  1204. /*
  1205. * XXX shouldn't update any times until successful
  1206. * completion of syscall, but too many callers depend
  1207. * on this.
  1208. *
  1209. * XXX similarly, too many callers depend on
  1210. * ext4_new_inode() setting the times, but error
  1211. * recovery deletes the inode, so the worst that can
  1212. * happen is that the times are slightly out of date
  1213. * and/or different from the directory change time.
  1214. */
  1215. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1216. ext4_update_dx_flag(dir);
  1217. dir->i_version++;
  1218. ext4_mark_inode_dirty(handle, dir);
  1219. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1220. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1221. if (err)
  1222. ext4_std_error(dir->i_sb, err);
  1223. return 0;
  1224. }
  1225. /*
  1226. * This converts a one block unindexed directory to a 3 block indexed
  1227. * directory, and adds the dentry to the indexed directory.
  1228. */
  1229. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1230. struct inode *inode, struct buffer_head *bh)
  1231. {
  1232. struct inode *dir = dentry->d_parent->d_inode;
  1233. const char *name = dentry->d_name.name;
  1234. int namelen = dentry->d_name.len;
  1235. struct buffer_head *bh2;
  1236. struct dx_root *root;
  1237. struct dx_frame frames[2], *frame;
  1238. struct dx_entry *entries;
  1239. struct ext4_dir_entry_2 *de, *de2;
  1240. char *data1, *top;
  1241. unsigned len;
  1242. int retval;
  1243. unsigned blocksize;
  1244. struct dx_hash_info hinfo;
  1245. ext4_lblk_t block;
  1246. struct fake_dirent *fde;
  1247. blocksize = dir->i_sb->s_blocksize;
  1248. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1249. retval = ext4_journal_get_write_access(handle, bh);
  1250. if (retval) {
  1251. ext4_std_error(dir->i_sb, retval);
  1252. brelse(bh);
  1253. return retval;
  1254. }
  1255. root = (struct dx_root *) bh->b_data;
  1256. /* The 0th block becomes the root, move the dirents out */
  1257. fde = &root->dotdot;
  1258. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1259. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1260. if ((char *) de >= (((char *) root) + blocksize)) {
  1261. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1262. brelse(bh);
  1263. return -EIO;
  1264. }
  1265. len = ((char *) root) + blocksize - (char *) de;
  1266. /* Allocate new block for the 0th block's dirents */
  1267. bh2 = ext4_append(handle, dir, &block, &retval);
  1268. if (!(bh2)) {
  1269. brelse(bh);
  1270. return retval;
  1271. }
  1272. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1273. data1 = bh2->b_data;
  1274. memcpy (data1, de, len);
  1275. de = (struct ext4_dir_entry_2 *) data1;
  1276. top = data1 + len;
  1277. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1278. de = de2;
  1279. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1280. blocksize);
  1281. /* Initialize the root; the dot dirents already exist */
  1282. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1283. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1284. blocksize);
  1285. memset (&root->info, 0, sizeof(root->info));
  1286. root->info.info_length = sizeof(root->info);
  1287. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1288. entries = root->entries;
  1289. dx_set_block(entries, 1);
  1290. dx_set_count(entries, 1);
  1291. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1292. /* Initialize as for dx_probe */
  1293. hinfo.hash_version = root->info.hash_version;
  1294. if (hinfo.hash_version <= DX_HASH_TEA)
  1295. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1296. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1297. ext4fs_dirhash(name, namelen, &hinfo);
  1298. frame = frames;
  1299. frame->entries = entries;
  1300. frame->at = entries;
  1301. frame->bh = bh;
  1302. bh = bh2;
  1303. ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1304. ext4_handle_dirty_metadata(handle, dir, bh);
  1305. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1306. if (!de) {
  1307. /*
  1308. * Even if the block split failed, we have to properly write
  1309. * out all the changes we did so far. Otherwise we can end up
  1310. * with corrupted filesystem.
  1311. */
  1312. ext4_mark_inode_dirty(handle, dir);
  1313. dx_release(frames);
  1314. return retval;
  1315. }
  1316. dx_release(frames);
  1317. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1318. brelse(bh);
  1319. return retval;
  1320. }
  1321. /*
  1322. * ext4_add_entry()
  1323. *
  1324. * adds a file entry to the specified directory, using the same
  1325. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1326. *
  1327. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1328. * may not sleep between calling this and putting something into
  1329. * the entry, as someone else might have used it while you slept.
  1330. */
  1331. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1332. struct inode *inode)
  1333. {
  1334. struct inode *dir = dentry->d_parent->d_inode;
  1335. struct buffer_head *bh;
  1336. struct ext4_dir_entry_2 *de;
  1337. struct super_block *sb;
  1338. int retval;
  1339. int dx_fallback=0;
  1340. unsigned blocksize;
  1341. ext4_lblk_t block, blocks;
  1342. sb = dir->i_sb;
  1343. blocksize = sb->s_blocksize;
  1344. if (!dentry->d_name.len)
  1345. return -EINVAL;
  1346. if (is_dx(dir)) {
  1347. retval = ext4_dx_add_entry(handle, dentry, inode);
  1348. if (!retval || (retval != ERR_BAD_DX_DIR))
  1349. return retval;
  1350. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1351. dx_fallback++;
  1352. ext4_mark_inode_dirty(handle, dir);
  1353. }
  1354. blocks = dir->i_size >> sb->s_blocksize_bits;
  1355. for (block = 0; block < blocks; block++) {
  1356. bh = ext4_bread(handle, dir, block, 0, &retval);
  1357. if(!bh)
  1358. return retval;
  1359. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1360. if (retval != -ENOSPC) {
  1361. brelse(bh);
  1362. return retval;
  1363. }
  1364. if (blocks == 1 && !dx_fallback &&
  1365. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1366. return make_indexed_dir(handle, dentry, inode, bh);
  1367. brelse(bh);
  1368. }
  1369. bh = ext4_append(handle, dir, &block, &retval);
  1370. if (!bh)
  1371. return retval;
  1372. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1373. de->inode = 0;
  1374. de->rec_len = ext4_rec_len_to_disk(blocksize, blocksize);
  1375. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1376. brelse(bh);
  1377. if (retval == 0)
  1378. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1379. return retval;
  1380. }
  1381. /*
  1382. * Returns 0 for success, or a negative error value
  1383. */
  1384. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1385. struct inode *inode)
  1386. {
  1387. struct dx_frame frames[2], *frame;
  1388. struct dx_entry *entries, *at;
  1389. struct dx_hash_info hinfo;
  1390. struct buffer_head *bh;
  1391. struct inode *dir = dentry->d_parent->d_inode;
  1392. struct super_block *sb = dir->i_sb;
  1393. struct ext4_dir_entry_2 *de;
  1394. int err;
  1395. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1396. if (!frame)
  1397. return err;
  1398. entries = frame->entries;
  1399. at = frame->at;
  1400. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1401. goto cleanup;
  1402. BUFFER_TRACE(bh, "get_write_access");
  1403. err = ext4_journal_get_write_access(handle, bh);
  1404. if (err)
  1405. goto journal_error;
  1406. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1407. if (err != -ENOSPC)
  1408. goto cleanup;
  1409. /* Block full, should compress but for now just split */
  1410. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1411. dx_get_count(entries), dx_get_limit(entries)));
  1412. /* Need to split index? */
  1413. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1414. ext4_lblk_t newblock;
  1415. unsigned icount = dx_get_count(entries);
  1416. int levels = frame - frames;
  1417. struct dx_entry *entries2;
  1418. struct dx_node *node2;
  1419. struct buffer_head *bh2;
  1420. if (levels && (dx_get_count(frames->entries) ==
  1421. dx_get_limit(frames->entries))) {
  1422. ext4_warning(sb, "Directory index full!");
  1423. err = -ENOSPC;
  1424. goto cleanup;
  1425. }
  1426. bh2 = ext4_append (handle, dir, &newblock, &err);
  1427. if (!(bh2))
  1428. goto cleanup;
  1429. node2 = (struct dx_node *)(bh2->b_data);
  1430. entries2 = node2->entries;
  1431. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  1432. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  1433. sb->s_blocksize);
  1434. BUFFER_TRACE(frame->bh, "get_write_access");
  1435. err = ext4_journal_get_write_access(handle, frame->bh);
  1436. if (err)
  1437. goto journal_error;
  1438. if (levels) {
  1439. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1440. unsigned hash2 = dx_get_hash(entries + icount1);
  1441. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1442. icount1, icount2));
  1443. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1444. err = ext4_journal_get_write_access(handle,
  1445. frames[0].bh);
  1446. if (err)
  1447. goto journal_error;
  1448. memcpy((char *) entries2, (char *) (entries + icount1),
  1449. icount2 * sizeof(struct dx_entry));
  1450. dx_set_count(entries, icount1);
  1451. dx_set_count(entries2, icount2);
  1452. dx_set_limit(entries2, dx_node_limit(dir));
  1453. /* Which index block gets the new entry? */
  1454. if (at - entries >= icount1) {
  1455. frame->at = at = at - entries - icount1 + entries2;
  1456. frame->entries = entries = entries2;
  1457. swap(frame->bh, bh2);
  1458. }
  1459. dx_insert_block(frames + 0, hash2, newblock);
  1460. dxtrace(dx_show_index("node", frames[1].entries));
  1461. dxtrace(dx_show_index("node",
  1462. ((struct dx_node *) bh2->b_data)->entries));
  1463. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1464. if (err)
  1465. goto journal_error;
  1466. brelse (bh2);
  1467. } else {
  1468. dxtrace(printk(KERN_DEBUG
  1469. "Creating second level index...\n"));
  1470. memcpy((char *) entries2, (char *) entries,
  1471. icount * sizeof(struct dx_entry));
  1472. dx_set_limit(entries2, dx_node_limit(dir));
  1473. /* Set up root */
  1474. dx_set_count(entries, 1);
  1475. dx_set_block(entries + 0, newblock);
  1476. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1477. /* Add new access path frame */
  1478. frame = frames + 1;
  1479. frame->at = at = at - entries + entries2;
  1480. frame->entries = entries = entries2;
  1481. frame->bh = bh2;
  1482. err = ext4_journal_get_write_access(handle,
  1483. frame->bh);
  1484. if (err)
  1485. goto journal_error;
  1486. }
  1487. err = ext4_handle_dirty_metadata(handle, dir, frames[0].bh);
  1488. if (err) {
  1489. ext4_std_error(inode->i_sb, err);
  1490. goto cleanup;
  1491. }
  1492. }
  1493. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1494. if (!de)
  1495. goto cleanup;
  1496. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1497. goto cleanup;
  1498. journal_error:
  1499. ext4_std_error(dir->i_sb, err);
  1500. cleanup:
  1501. if (bh)
  1502. brelse(bh);
  1503. dx_release(frames);
  1504. return err;
  1505. }
  1506. /*
  1507. * ext4_delete_entry deletes a directory entry by merging it with the
  1508. * previous entry
  1509. */
  1510. static int ext4_delete_entry(handle_t *handle,
  1511. struct inode *dir,
  1512. struct ext4_dir_entry_2 *de_del,
  1513. struct buffer_head *bh)
  1514. {
  1515. struct ext4_dir_entry_2 *de, *pde;
  1516. unsigned int blocksize = dir->i_sb->s_blocksize;
  1517. int i, err;
  1518. i = 0;
  1519. pde = NULL;
  1520. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1521. while (i < bh->b_size) {
  1522. if (ext4_check_dir_entry(dir, NULL, de, bh, i))
  1523. return -EIO;
  1524. if (de == de_del) {
  1525. BUFFER_TRACE(bh, "get_write_access");
  1526. err = ext4_journal_get_write_access(handle, bh);
  1527. if (unlikely(err)) {
  1528. ext4_std_error(dir->i_sb, err);
  1529. return err;
  1530. }
  1531. if (pde)
  1532. pde->rec_len = ext4_rec_len_to_disk(
  1533. ext4_rec_len_from_disk(pde->rec_len,
  1534. blocksize) +
  1535. ext4_rec_len_from_disk(de->rec_len,
  1536. blocksize),
  1537. blocksize);
  1538. else
  1539. de->inode = 0;
  1540. dir->i_version++;
  1541. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1542. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1543. if (unlikely(err)) {
  1544. ext4_std_error(dir->i_sb, err);
  1545. return err;
  1546. }
  1547. return 0;
  1548. }
  1549. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  1550. pde = de;
  1551. de = ext4_next_entry(de, blocksize);
  1552. }
  1553. return -ENOENT;
  1554. }
  1555. /*
  1556. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1557. * since this indicates that nlinks count was previously 1.
  1558. */
  1559. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1560. {
  1561. inc_nlink(inode);
  1562. if (is_dx(inode) && inode->i_nlink > 1) {
  1563. /* limit is 16-bit i_links_count */
  1564. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1565. inode->i_nlink = 1;
  1566. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1567. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1568. }
  1569. }
  1570. }
  1571. /*
  1572. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1573. * directory has >EXT4_LINK_MAX subdirs.
  1574. */
  1575. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1576. {
  1577. drop_nlink(inode);
  1578. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1579. inc_nlink(inode);
  1580. }
  1581. static int ext4_add_nondir(handle_t *handle,
  1582. struct dentry *dentry, struct inode *inode)
  1583. {
  1584. int err = ext4_add_entry(handle, dentry, inode);
  1585. if (!err) {
  1586. ext4_mark_inode_dirty(handle, inode);
  1587. d_instantiate(dentry, inode);
  1588. unlock_new_inode(inode);
  1589. return 0;
  1590. }
  1591. drop_nlink(inode);
  1592. unlock_new_inode(inode);
  1593. iput(inode);
  1594. return err;
  1595. }
  1596. /*
  1597. * By the time this is called, we already have created
  1598. * the directory cache entry for the new file, but it
  1599. * is so far negative - it has no inode.
  1600. *
  1601. * If the create succeeds, we fill in the inode information
  1602. * with d_instantiate().
  1603. */
  1604. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1605. struct nameidata *nd)
  1606. {
  1607. handle_t *handle;
  1608. struct inode *inode;
  1609. int err, retries = 0;
  1610. dquot_initialize(dir);
  1611. retry:
  1612. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1613. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1614. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1615. if (IS_ERR(handle))
  1616. return PTR_ERR(handle);
  1617. if (IS_DIRSYNC(dir))
  1618. ext4_handle_sync(handle);
  1619. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1620. err = PTR_ERR(inode);
  1621. if (!IS_ERR(inode)) {
  1622. inode->i_op = &ext4_file_inode_operations;
  1623. inode->i_fop = &ext4_file_operations;
  1624. ext4_set_aops(inode);
  1625. err = ext4_add_nondir(handle, dentry, inode);
  1626. }
  1627. ext4_journal_stop(handle);
  1628. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1629. goto retry;
  1630. return err;
  1631. }
  1632. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1633. int mode, dev_t rdev)
  1634. {
  1635. handle_t *handle;
  1636. struct inode *inode;
  1637. int err, retries = 0;
  1638. if (!new_valid_dev(rdev))
  1639. return -EINVAL;
  1640. dquot_initialize(dir);
  1641. retry:
  1642. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1643. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1644. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1645. if (IS_ERR(handle))
  1646. return PTR_ERR(handle);
  1647. if (IS_DIRSYNC(dir))
  1648. ext4_handle_sync(handle);
  1649. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1650. err = PTR_ERR(inode);
  1651. if (!IS_ERR(inode)) {
  1652. init_special_inode(inode, inode->i_mode, rdev);
  1653. #ifdef CONFIG_EXT4_FS_XATTR
  1654. inode->i_op = &ext4_special_inode_operations;
  1655. #endif
  1656. err = ext4_add_nondir(handle, dentry, inode);
  1657. }
  1658. ext4_journal_stop(handle);
  1659. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1660. goto retry;
  1661. return err;
  1662. }
  1663. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1664. {
  1665. handle_t *handle;
  1666. struct inode *inode;
  1667. struct buffer_head *dir_block = NULL;
  1668. struct ext4_dir_entry_2 *de;
  1669. unsigned int blocksize = dir->i_sb->s_blocksize;
  1670. int err, retries = 0;
  1671. if (EXT4_DIR_LINK_MAX(dir))
  1672. return -EMLINK;
  1673. dquot_initialize(dir);
  1674. retry:
  1675. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1676. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1677. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1678. if (IS_ERR(handle))
  1679. return PTR_ERR(handle);
  1680. if (IS_DIRSYNC(dir))
  1681. ext4_handle_sync(handle);
  1682. inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
  1683. &dentry->d_name, 0);
  1684. err = PTR_ERR(inode);
  1685. if (IS_ERR(inode))
  1686. goto out_stop;
  1687. inode->i_op = &ext4_dir_inode_operations;
  1688. inode->i_fop = &ext4_dir_operations;
  1689. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1690. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1691. if (!dir_block)
  1692. goto out_clear_inode;
  1693. BUFFER_TRACE(dir_block, "get_write_access");
  1694. err = ext4_journal_get_write_access(handle, dir_block);
  1695. if (err)
  1696. goto out_clear_inode;
  1697. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1698. de->inode = cpu_to_le32(inode->i_ino);
  1699. de->name_len = 1;
  1700. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  1701. blocksize);
  1702. strcpy(de->name, ".");
  1703. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1704. de = ext4_next_entry(de, blocksize);
  1705. de->inode = cpu_to_le32(dir->i_ino);
  1706. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(1),
  1707. blocksize);
  1708. de->name_len = 2;
  1709. strcpy(de->name, "..");
  1710. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1711. inode->i_nlink = 2;
  1712. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  1713. err = ext4_handle_dirty_metadata(handle, inode, dir_block);
  1714. if (err)
  1715. goto out_clear_inode;
  1716. err = ext4_mark_inode_dirty(handle, inode);
  1717. if (!err)
  1718. err = ext4_add_entry(handle, dentry, inode);
  1719. if (err) {
  1720. out_clear_inode:
  1721. clear_nlink(inode);
  1722. unlock_new_inode(inode);
  1723. ext4_mark_inode_dirty(handle, inode);
  1724. iput(inode);
  1725. goto out_stop;
  1726. }
  1727. ext4_inc_count(handle, dir);
  1728. ext4_update_dx_flag(dir);
  1729. err = ext4_mark_inode_dirty(handle, dir);
  1730. if (err)
  1731. goto out_clear_inode;
  1732. d_instantiate(dentry, inode);
  1733. unlock_new_inode(inode);
  1734. out_stop:
  1735. brelse(dir_block);
  1736. ext4_journal_stop(handle);
  1737. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1738. goto retry;
  1739. return err;
  1740. }
  1741. /*
  1742. * routine to check that the specified directory is empty (for rmdir)
  1743. */
  1744. static int empty_dir(struct inode *inode)
  1745. {
  1746. unsigned int offset;
  1747. struct buffer_head *bh;
  1748. struct ext4_dir_entry_2 *de, *de1;
  1749. struct super_block *sb;
  1750. int err = 0;
  1751. sb = inode->i_sb;
  1752. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1753. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1754. if (err)
  1755. EXT4_ERROR_INODE(inode,
  1756. "error %d reading directory lblock 0", err);
  1757. else
  1758. ext4_warning(inode->i_sb,
  1759. "bad directory (dir #%lu) - no data block",
  1760. inode->i_ino);
  1761. return 1;
  1762. }
  1763. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1764. de1 = ext4_next_entry(de, sb->s_blocksize);
  1765. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1766. !le32_to_cpu(de1->inode) ||
  1767. strcmp(".", de->name) ||
  1768. strcmp("..", de1->name)) {
  1769. ext4_warning(inode->i_sb,
  1770. "bad directory (dir #%lu) - no `.' or `..'",
  1771. inode->i_ino);
  1772. brelse(bh);
  1773. return 1;
  1774. }
  1775. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  1776. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  1777. de = ext4_next_entry(de1, sb->s_blocksize);
  1778. while (offset < inode->i_size) {
  1779. if (!bh ||
  1780. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1781. unsigned int lblock;
  1782. err = 0;
  1783. brelse(bh);
  1784. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  1785. bh = ext4_bread(NULL, inode, lblock, 0, &err);
  1786. if (!bh) {
  1787. if (err)
  1788. EXT4_ERROR_INODE(inode,
  1789. "error %d reading directory "
  1790. "lblock %u", err, lblock);
  1791. offset += sb->s_blocksize;
  1792. continue;
  1793. }
  1794. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1795. }
  1796. if (ext4_check_dir_entry(inode, NULL, de, bh, offset)) {
  1797. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1798. sb->s_blocksize);
  1799. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1800. continue;
  1801. }
  1802. if (le32_to_cpu(de->inode)) {
  1803. brelse(bh);
  1804. return 0;
  1805. }
  1806. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  1807. de = ext4_next_entry(de, sb->s_blocksize);
  1808. }
  1809. brelse(bh);
  1810. return 1;
  1811. }
  1812. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1813. * such inodes, starting at the superblock, in case we crash before the
  1814. * file is closed/deleted, or in case the inode truncate spans multiple
  1815. * transactions and the last transaction is not recovered after a crash.
  1816. *
  1817. * At filesystem recovery time, we walk this list deleting unlinked
  1818. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1819. */
  1820. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1821. {
  1822. struct super_block *sb = inode->i_sb;
  1823. struct ext4_iloc iloc;
  1824. int err = 0, rc;
  1825. if (!ext4_handle_valid(handle))
  1826. return 0;
  1827. mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
  1828. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1829. goto out_unlock;
  1830. /* Orphan handling is only valid for files with data blocks
  1831. * being truncated, or files being unlinked. */
  1832. /* @@@ FIXME: Observation from aviro:
  1833. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1834. * here (on s_orphan_lock), so race with ext4_link() which might bump
  1835. * ->i_nlink. For, say it, character device. Not a regular file,
  1836. * not a directory, not a symlink and ->i_nlink > 0.
  1837. *
  1838. * tytso, 4/25/2009: I'm not sure how that could happen;
  1839. * shouldn't the fs core protect us from these sort of
  1840. * unlink()/link() races?
  1841. */
  1842. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1843. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1844. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1845. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1846. if (err)
  1847. goto out_unlock;
  1848. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1849. if (err)
  1850. goto out_unlock;
  1851. /*
  1852. * Due to previous errors inode may be already a part of on-disk
  1853. * orphan list. If so skip on-disk list modification.
  1854. */
  1855. if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
  1856. (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
  1857. goto mem_insert;
  1858. /* Insert this inode at the head of the on-disk orphan list... */
  1859. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1860. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1861. err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
  1862. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1863. if (!err)
  1864. err = rc;
  1865. /* Only add to the head of the in-memory list if all the
  1866. * previous operations succeeded. If the orphan_add is going to
  1867. * fail (possibly taking the journal offline), we can't risk
  1868. * leaving the inode on the orphan list: stray orphan-list
  1869. * entries can cause panics at unmount time.
  1870. *
  1871. * This is safe: on error we're going to ignore the orphan list
  1872. * anyway on the next recovery. */
  1873. mem_insert:
  1874. if (!err)
  1875. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1876. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1877. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1878. inode->i_ino, NEXT_ORPHAN(inode));
  1879. out_unlock:
  1880. mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
  1881. ext4_std_error(inode->i_sb, err);
  1882. return err;
  1883. }
  1884. /*
  1885. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1886. * of such inodes stored on disk, because it is finally being cleaned up.
  1887. */
  1888. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1889. {
  1890. struct list_head *prev;
  1891. struct ext4_inode_info *ei = EXT4_I(inode);
  1892. struct ext4_sb_info *sbi;
  1893. __u32 ino_next;
  1894. struct ext4_iloc iloc;
  1895. int err = 0;
  1896. /* ext4_handle_valid() assumes a valid handle_t pointer */
  1897. if (handle && !ext4_handle_valid(handle))
  1898. return 0;
  1899. mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  1900. if (list_empty(&ei->i_orphan))
  1901. goto out;
  1902. ino_next = NEXT_ORPHAN(inode);
  1903. prev = ei->i_orphan.prev;
  1904. sbi = EXT4_SB(inode->i_sb);
  1905. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1906. list_del_init(&ei->i_orphan);
  1907. /* If we're on an error path, we may not have a valid
  1908. * transaction handle with which to update the orphan list on
  1909. * disk, but we still need to remove the inode from the linked
  1910. * list in memory. */
  1911. if (sbi->s_journal && !handle)
  1912. goto out;
  1913. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1914. if (err)
  1915. goto out_err;
  1916. if (prev == &sbi->s_orphan) {
  1917. jbd_debug(4, "superblock will point to %u\n", ino_next);
  1918. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1919. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1920. if (err)
  1921. goto out_brelse;
  1922. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1923. err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
  1924. } else {
  1925. struct ext4_iloc iloc2;
  1926. struct inode *i_prev =
  1927. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1928. jbd_debug(4, "orphan inode %lu will point to %u\n",
  1929. i_prev->i_ino, ino_next);
  1930. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1931. if (err)
  1932. goto out_brelse;
  1933. NEXT_ORPHAN(i_prev) = ino_next;
  1934. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1935. }
  1936. if (err)
  1937. goto out_brelse;
  1938. NEXT_ORPHAN(inode) = 0;
  1939. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1940. out_err:
  1941. ext4_std_error(inode->i_sb, err);
  1942. out:
  1943. mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  1944. return err;
  1945. out_brelse:
  1946. brelse(iloc.bh);
  1947. goto out_err;
  1948. }
  1949. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  1950. {
  1951. int retval;
  1952. struct inode *inode;
  1953. struct buffer_head *bh;
  1954. struct ext4_dir_entry_2 *de;
  1955. handle_t *handle;
  1956. /* Initialize quotas before so that eventual writes go in
  1957. * separate transaction */
  1958. dquot_initialize(dir);
  1959. dquot_initialize(dentry->d_inode);
  1960. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1961. if (IS_ERR(handle))
  1962. return PTR_ERR(handle);
  1963. retval = -ENOENT;
  1964. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1965. if (!bh)
  1966. goto end_rmdir;
  1967. if (IS_DIRSYNC(dir))
  1968. ext4_handle_sync(handle);
  1969. inode = dentry->d_inode;
  1970. retval = -EIO;
  1971. if (le32_to_cpu(de->inode) != inode->i_ino)
  1972. goto end_rmdir;
  1973. retval = -ENOTEMPTY;
  1974. if (!empty_dir(inode))
  1975. goto end_rmdir;
  1976. retval = ext4_delete_entry(handle, dir, de, bh);
  1977. if (retval)
  1978. goto end_rmdir;
  1979. if (!EXT4_DIR_LINK_EMPTY(inode))
  1980. ext4_warning(inode->i_sb,
  1981. "empty directory has too many links (%d)",
  1982. inode->i_nlink);
  1983. inode->i_version++;
  1984. clear_nlink(inode);
  1985. /* There's no need to set i_disksize: the fact that i_nlink is
  1986. * zero will ensure that the right thing happens during any
  1987. * recovery. */
  1988. inode->i_size = 0;
  1989. ext4_orphan_add(handle, inode);
  1990. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1991. ext4_mark_inode_dirty(handle, inode);
  1992. ext4_dec_count(handle, dir);
  1993. ext4_update_dx_flag(dir);
  1994. ext4_mark_inode_dirty(handle, dir);
  1995. end_rmdir:
  1996. ext4_journal_stop(handle);
  1997. brelse(bh);
  1998. return retval;
  1999. }
  2000. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  2001. {
  2002. int retval;
  2003. struct inode *inode;
  2004. struct buffer_head *bh;
  2005. struct ext4_dir_entry_2 *de;
  2006. handle_t *handle;
  2007. trace_ext4_unlink_enter(dir, dentry);
  2008. /* Initialize quotas before so that eventual writes go
  2009. * in separate transaction */
  2010. dquot_initialize(dir);
  2011. dquot_initialize(dentry->d_inode);
  2012. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  2013. if (IS_ERR(handle))
  2014. return PTR_ERR(handle);
  2015. if (IS_DIRSYNC(dir))
  2016. ext4_handle_sync(handle);
  2017. retval = -ENOENT;
  2018. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  2019. if (!bh)
  2020. goto end_unlink;
  2021. inode = dentry->d_inode;
  2022. retval = -EIO;
  2023. if (le32_to_cpu(de->inode) != inode->i_ino)
  2024. goto end_unlink;
  2025. if (!inode->i_nlink) {
  2026. ext4_warning(inode->i_sb,
  2027. "Deleting nonexistent file (%lu), %d",
  2028. inode->i_ino, inode->i_nlink);
  2029. inode->i_nlink = 1;
  2030. }
  2031. retval = ext4_delete_entry(handle, dir, de, bh);
  2032. if (retval)
  2033. goto end_unlink;
  2034. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  2035. ext4_update_dx_flag(dir);
  2036. ext4_mark_inode_dirty(handle, dir);
  2037. drop_nlink(inode);
  2038. if (!inode->i_nlink)
  2039. ext4_orphan_add(handle, inode);
  2040. inode->i_ctime = ext4_current_time(inode);
  2041. ext4_mark_inode_dirty(handle, inode);
  2042. retval = 0;
  2043. end_unlink:
  2044. ext4_journal_stop(handle);
  2045. brelse(bh);
  2046. trace_ext4_unlink_exit(dentry, retval);
  2047. return retval;
  2048. }
  2049. static int ext4_symlink(struct inode *dir,
  2050. struct dentry *dentry, const char *symname)
  2051. {
  2052. handle_t *handle;
  2053. struct inode *inode;
  2054. int l, err, retries = 0;
  2055. int credits;
  2056. l = strlen(symname)+1;
  2057. if (l > dir->i_sb->s_blocksize)
  2058. return -ENAMETOOLONG;
  2059. dquot_initialize(dir);
  2060. if (l > EXT4_N_BLOCKS * 4) {
  2061. /*
  2062. * For non-fast symlinks, we just allocate inode and put it on
  2063. * orphan list in the first transaction => we need bitmap,
  2064. * group descriptor, sb, inode block, quota blocks, and
  2065. * possibly selinux xattr blocks.
  2066. */
  2067. credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2068. EXT4_XATTR_TRANS_BLOCKS;
  2069. } else {
  2070. /*
  2071. * Fast symlink. We have to add entry to directory
  2072. * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
  2073. * allocate new inode (bitmap, group descriptor, inode block,
  2074. * quota blocks, sb is already counted in previous macros).
  2075. */
  2076. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2077. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  2078. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
  2079. }
  2080. retry:
  2081. handle = ext4_journal_start(dir, credits);
  2082. if (IS_ERR(handle))
  2083. return PTR_ERR(handle);
  2084. if (IS_DIRSYNC(dir))
  2085. ext4_handle_sync(handle);
  2086. inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
  2087. &dentry->d_name, 0);
  2088. err = PTR_ERR(inode);
  2089. if (IS_ERR(inode))
  2090. goto out_stop;
  2091. if (l > EXT4_N_BLOCKS * 4) {
  2092. inode->i_op = &ext4_symlink_inode_operations;
  2093. ext4_set_aops(inode);
  2094. /*
  2095. * We cannot call page_symlink() with transaction started
  2096. * because it calls into ext4_write_begin() which can wait
  2097. * for transaction commit if we are running out of space
  2098. * and thus we deadlock. So we have to stop transaction now
  2099. * and restart it when symlink contents is written.
  2100. *
  2101. * To keep fs consistent in case of crash, we have to put inode
  2102. * to orphan list in the mean time.
  2103. */
  2104. drop_nlink(inode);
  2105. err = ext4_orphan_add(handle, inode);
  2106. ext4_journal_stop(handle);
  2107. if (err)
  2108. goto err_drop_inode;
  2109. err = __page_symlink(inode, symname, l, 1);
  2110. if (err)
  2111. goto err_drop_inode;
  2112. /*
  2113. * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
  2114. * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
  2115. */
  2116. handle = ext4_journal_start(dir,
  2117. EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2118. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
  2119. if (IS_ERR(handle)) {
  2120. err = PTR_ERR(handle);
  2121. goto err_drop_inode;
  2122. }
  2123. inc_nlink(inode);
  2124. err = ext4_orphan_del(handle, inode);
  2125. if (err) {
  2126. ext4_journal_stop(handle);
  2127. clear_nlink(inode);
  2128. goto err_drop_inode;
  2129. }
  2130. } else {
  2131. /* clear the extent format for fast symlink */
  2132. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2133. inode->i_op = &ext4_fast_symlink_inode_operations;
  2134. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2135. inode->i_size = l-1;
  2136. }
  2137. EXT4_I(inode)->i_disksize = inode->i_size;
  2138. err = ext4_add_nondir(handle, dentry, inode);
  2139. out_stop:
  2140. ext4_journal_stop(handle);
  2141. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2142. goto retry;
  2143. return err;
  2144. err_drop_inode:
  2145. unlock_new_inode(inode);
  2146. iput(inode);
  2147. return err;
  2148. }
  2149. static int ext4_link(struct dentry *old_dentry,
  2150. struct inode *dir, struct dentry *dentry)
  2151. {
  2152. handle_t *handle;
  2153. struct inode *inode = old_dentry->d_inode;
  2154. int err, retries = 0;
  2155. if (inode->i_nlink >= EXT4_LINK_MAX)
  2156. return -EMLINK;
  2157. dquot_initialize(dir);
  2158. retry:
  2159. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2160. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2161. if (IS_ERR(handle))
  2162. return PTR_ERR(handle);
  2163. if (IS_DIRSYNC(dir))
  2164. ext4_handle_sync(handle);
  2165. inode->i_ctime = ext4_current_time(inode);
  2166. ext4_inc_count(handle, inode);
  2167. ihold(inode);
  2168. err = ext4_add_entry(handle, dentry, inode);
  2169. if (!err) {
  2170. ext4_mark_inode_dirty(handle, inode);
  2171. d_instantiate(dentry, inode);
  2172. } else {
  2173. drop_nlink(inode);
  2174. iput(inode);
  2175. }
  2176. ext4_journal_stop(handle);
  2177. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2178. goto retry;
  2179. return err;
  2180. }
  2181. #define PARENT_INO(buffer, size) \
  2182. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
  2183. /*
  2184. * Anybody can rename anything with this: the permission checks are left to the
  2185. * higher-level routines.
  2186. */
  2187. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2188. struct inode *new_dir, struct dentry *new_dentry)
  2189. {
  2190. handle_t *handle;
  2191. struct inode *old_inode, *new_inode;
  2192. struct buffer_head *old_bh, *new_bh, *dir_bh;
  2193. struct ext4_dir_entry_2 *old_de, *new_de;
  2194. int retval, force_da_alloc = 0;
  2195. dquot_initialize(old_dir);
  2196. dquot_initialize(new_dir);
  2197. old_bh = new_bh = dir_bh = NULL;
  2198. /* Initialize quotas before so that eventual writes go
  2199. * in separate transaction */
  2200. if (new_dentry->d_inode)
  2201. dquot_initialize(new_dentry->d_inode);
  2202. handle = ext4_journal_start(old_dir, 2 *
  2203. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2204. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2205. if (IS_ERR(handle))
  2206. return PTR_ERR(handle);
  2207. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2208. ext4_handle_sync(handle);
  2209. old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
  2210. /*
  2211. * Check for inode number is _not_ due to possible IO errors.
  2212. * We might rmdir the source, keep it as pwd of some process
  2213. * and merrily kill the link to whatever was created under the
  2214. * same name. Goodbye sticky bit ;-<
  2215. */
  2216. old_inode = old_dentry->d_inode;
  2217. retval = -ENOENT;
  2218. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2219. goto end_rename;
  2220. new_inode = new_dentry->d_inode;
  2221. new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
  2222. if (new_bh) {
  2223. if (!new_inode) {
  2224. brelse(new_bh);
  2225. new_bh = NULL;
  2226. }
  2227. }
  2228. if (S_ISDIR(old_inode->i_mode)) {
  2229. if (new_inode) {
  2230. retval = -ENOTEMPTY;
  2231. if (!empty_dir(new_inode))
  2232. goto end_rename;
  2233. }
  2234. retval = -EIO;
  2235. dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
  2236. if (!dir_bh)
  2237. goto end_rename;
  2238. if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
  2239. old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
  2240. goto end_rename;
  2241. retval = -EMLINK;
  2242. if (!new_inode && new_dir != old_dir &&
  2243. EXT4_DIR_LINK_MAX(new_dir))
  2244. goto end_rename;
  2245. BUFFER_TRACE(dir_bh, "get_write_access");
  2246. retval = ext4_journal_get_write_access(handle, dir_bh);
  2247. if (retval)
  2248. goto end_rename;
  2249. }
  2250. if (!new_bh) {
  2251. retval = ext4_add_entry(handle, new_dentry, old_inode);
  2252. if (retval)
  2253. goto end_rename;
  2254. } else {
  2255. BUFFER_TRACE(new_bh, "get write access");
  2256. retval = ext4_journal_get_write_access(handle, new_bh);
  2257. if (retval)
  2258. goto end_rename;
  2259. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2260. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2261. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2262. new_de->file_type = old_de->file_type;
  2263. new_dir->i_version++;
  2264. new_dir->i_ctime = new_dir->i_mtime =
  2265. ext4_current_time(new_dir);
  2266. ext4_mark_inode_dirty(handle, new_dir);
  2267. BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
  2268. retval = ext4_handle_dirty_metadata(handle, new_dir, new_bh);
  2269. if (unlikely(retval)) {
  2270. ext4_std_error(new_dir->i_sb, retval);
  2271. goto end_rename;
  2272. }
  2273. brelse(new_bh);
  2274. new_bh = NULL;
  2275. }
  2276. /*
  2277. * Like most other Unix systems, set the ctime for inodes on a
  2278. * rename.
  2279. */
  2280. old_inode->i_ctime = ext4_current_time(old_inode);
  2281. ext4_mark_inode_dirty(handle, old_inode);
  2282. /*
  2283. * ok, that's it
  2284. */
  2285. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2286. old_de->name_len != old_dentry->d_name.len ||
  2287. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2288. (retval = ext4_delete_entry(handle, old_dir,
  2289. old_de, old_bh)) == -ENOENT) {
  2290. /* old_de could have moved from under us during htree split, so
  2291. * make sure that we are deleting the right entry. We might
  2292. * also be pointing to a stale entry in the unused part of
  2293. * old_bh so just checking inum and the name isn't enough. */
  2294. struct buffer_head *old_bh2;
  2295. struct ext4_dir_entry_2 *old_de2;
  2296. old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
  2297. if (old_bh2) {
  2298. retval = ext4_delete_entry(handle, old_dir,
  2299. old_de2, old_bh2);
  2300. brelse(old_bh2);
  2301. }
  2302. }
  2303. if (retval) {
  2304. ext4_warning(old_dir->i_sb,
  2305. "Deleting old file (%lu), %d, error=%d",
  2306. old_dir->i_ino, old_dir->i_nlink, retval);
  2307. }
  2308. if (new_inode) {
  2309. ext4_dec_count(handle, new_inode);
  2310. new_inode->i_ctime = ext4_current_time(new_inode);
  2311. }
  2312. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2313. ext4_update_dx_flag(old_dir);
  2314. if (dir_bh) {
  2315. PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
  2316. cpu_to_le32(new_dir->i_ino);
  2317. BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
  2318. retval = ext4_handle_dirty_metadata(handle, old_inode, dir_bh);
  2319. if (retval) {
  2320. ext4_std_error(old_dir->i_sb, retval);
  2321. goto end_rename;
  2322. }
  2323. ext4_dec_count(handle, old_dir);
  2324. if (new_inode) {
  2325. /* checked empty_dir above, can't have another parent,
  2326. * ext4_dec_count() won't work for many-linked dirs */
  2327. new_inode->i_nlink = 0;
  2328. } else {
  2329. ext4_inc_count(handle, new_dir);
  2330. ext4_update_dx_flag(new_dir);
  2331. ext4_mark_inode_dirty(handle, new_dir);
  2332. }
  2333. }
  2334. ext4_mark_inode_dirty(handle, old_dir);
  2335. if (new_inode) {
  2336. ext4_mark_inode_dirty(handle, new_inode);
  2337. if (!new_inode->i_nlink)
  2338. ext4_orphan_add(handle, new_inode);
  2339. if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
  2340. force_da_alloc = 1;
  2341. }
  2342. retval = 0;
  2343. end_rename:
  2344. brelse(dir_bh);
  2345. brelse(old_bh);
  2346. brelse(new_bh);
  2347. ext4_journal_stop(handle);
  2348. if (retval == 0 && force_da_alloc)
  2349. ext4_alloc_da_blocks(old_inode);
  2350. return retval;
  2351. }
  2352. /*
  2353. * directories can handle most operations...
  2354. */
  2355. const struct inode_operations ext4_dir_inode_operations = {
  2356. .create = ext4_create,
  2357. .lookup = ext4_lookup,
  2358. .link = ext4_link,
  2359. .unlink = ext4_unlink,
  2360. .symlink = ext4_symlink,
  2361. .mkdir = ext4_mkdir,
  2362. .rmdir = ext4_rmdir,
  2363. .mknod = ext4_mknod,
  2364. .rename = ext4_rename,
  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. .check_acl = ext4_check_acl,
  2373. .fiemap = ext4_fiemap,
  2374. };
  2375. const struct inode_operations ext4_special_inode_operations = {
  2376. .setattr = ext4_setattr,
  2377. #ifdef CONFIG_EXT4_FS_XATTR
  2378. .setxattr = generic_setxattr,
  2379. .getxattr = generic_getxattr,
  2380. .listxattr = ext4_listxattr,
  2381. .removexattr = generic_removexattr,
  2382. #endif
  2383. .check_acl = ext4_check_acl,
  2384. };