namei.c 67 KB

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