namei.c 66 KB

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