namei.c 64 KB

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