namei.c 69 KB

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