namei.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * namei.c
  5. *
  6. * Create and rename file, directory, symlinks
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * Portions of this code from linux/fs/ext3/dir.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/dir.c
  20. *
  21. * Copyright (C) 1991, 1992 Linux Torvalds
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public
  25. * License as published by the Free Software Foundation; either
  26. * version 2 of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public
  34. * License along with this program; if not, write to the
  35. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  36. * Boston, MA 021110-1307, USA.
  37. */
  38. #include <linux/fs.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/highmem.h>
  42. #define MLOG_MASK_PREFIX ML_NAMEI
  43. #include <cluster/masklog.h>
  44. #include "ocfs2.h"
  45. #include "alloc.h"
  46. #include "dcache.h"
  47. #include "dir.h"
  48. #include "dlmglue.h"
  49. #include "extent_map.h"
  50. #include "file.h"
  51. #include "inode.h"
  52. #include "journal.h"
  53. #include "namei.h"
  54. #include "suballoc.h"
  55. #include "super.h"
  56. #include "symlink.h"
  57. #include "sysfile.h"
  58. #include "uptodate.h"
  59. #include "vote.h"
  60. #include "buffer_head_io.h"
  61. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  62. struct inode *dir,
  63. struct dentry *dentry, int mode,
  64. dev_t dev,
  65. struct buffer_head **new_fe_bh,
  66. struct buffer_head *parent_fe_bh,
  67. handle_t *handle,
  68. struct inode **ret_inode,
  69. struct ocfs2_alloc_context *inode_ac);
  70. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  71. struct inode **ret_orphan_dir,
  72. struct inode *inode,
  73. char *name,
  74. struct buffer_head **de_bh);
  75. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  76. handle_t *handle,
  77. struct inode *inode,
  78. struct ocfs2_dinode *fe,
  79. char *name,
  80. struct buffer_head *de_bh,
  81. struct inode *orphan_dir_inode);
  82. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  83. handle_t *handle,
  84. struct inode *inode,
  85. const char *symname);
  86. /* An orphan dir name is an 8 byte value, printed as a hex string */
  87. #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
  88. static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
  89. struct nameidata *nd)
  90. {
  91. int status;
  92. u64 blkno;
  93. struct inode *inode = NULL;
  94. struct dentry *ret;
  95. struct ocfs2_inode_info *oi;
  96. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  97. dentry->d_name.len, dentry->d_name.name);
  98. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  99. ret = ERR_PTR(-ENAMETOOLONG);
  100. goto bail;
  101. }
  102. mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
  103. dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  104. status = ocfs2_meta_lock(dir, NULL, 0);
  105. if (status < 0) {
  106. if (status != -ENOENT)
  107. mlog_errno(status);
  108. ret = ERR_PTR(status);
  109. goto bail;
  110. }
  111. status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
  112. dentry->d_name.len, &blkno);
  113. if (status < 0)
  114. goto bail_add;
  115. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
  116. if (IS_ERR(inode)) {
  117. ret = ERR_PTR(-EACCES);
  118. goto bail_unlock;
  119. }
  120. oi = OCFS2_I(inode);
  121. /* Clear any orphaned state... If we were able to look up the
  122. * inode from a directory, it certainly can't be orphaned. We
  123. * might have the bad state from a node which intended to
  124. * orphan this inode but crashed before it could commit the
  125. * unlink. */
  126. spin_lock(&oi->ip_lock);
  127. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  128. spin_unlock(&oi->ip_lock);
  129. bail_add:
  130. dentry->d_op = &ocfs2_dentry_ops;
  131. ret = d_splice_alias(inode, dentry);
  132. if (inode) {
  133. /*
  134. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  135. * dentry, it will d_move() it on top of ourse. The
  136. * return value will indicate this however, so in
  137. * those cases, we switch them around for the locking
  138. * code.
  139. *
  140. * NOTE: This dentry already has ->d_op set from
  141. * ocfs2_get_parent() and ocfs2_get_dentry()
  142. */
  143. if (ret)
  144. dentry = ret;
  145. status = ocfs2_dentry_attach_lock(dentry, inode,
  146. OCFS2_I(dir)->ip_blkno);
  147. if (status) {
  148. mlog_errno(status);
  149. ret = ERR_PTR(status);
  150. goto bail_unlock;
  151. }
  152. }
  153. bail_unlock:
  154. /* Don't drop the cluster lock until *after* the d_add --
  155. * unlink on another node will message us to remove that
  156. * dentry under this lock so otherwise we can race this with
  157. * the vote thread and have a stale dentry. */
  158. ocfs2_meta_unlock(dir, 0);
  159. bail:
  160. mlog_exit_ptr(ret);
  161. return ret;
  162. }
  163. static int ocfs2_mknod(struct inode *dir,
  164. struct dentry *dentry,
  165. int mode,
  166. dev_t dev)
  167. {
  168. int status = 0;
  169. struct buffer_head *parent_fe_bh = NULL;
  170. handle_t *handle = NULL;
  171. struct ocfs2_super *osb;
  172. struct ocfs2_dinode *dirfe;
  173. struct buffer_head *new_fe_bh = NULL;
  174. struct buffer_head *de_bh = NULL;
  175. struct inode *inode = NULL;
  176. struct ocfs2_alloc_context *inode_ac = NULL;
  177. struct ocfs2_alloc_context *data_ac = NULL;
  178. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  179. (unsigned long)dev, dentry->d_name.len,
  180. dentry->d_name.name);
  181. /* get our super block */
  182. osb = OCFS2_SB(dir->i_sb);
  183. status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
  184. if (status < 0) {
  185. if (status != -ENOENT)
  186. mlog_errno(status);
  187. return status;
  188. }
  189. if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
  190. status = -EMLINK;
  191. goto leave;
  192. }
  193. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  194. if (!dirfe->i_links_count) {
  195. /* can't make a file in a deleted directory. */
  196. status = -ENOENT;
  197. goto leave;
  198. }
  199. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  200. dentry->d_name.len);
  201. if (status)
  202. goto leave;
  203. /* get a spot inside the dir. */
  204. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  205. dentry->d_name.name,
  206. dentry->d_name.len, &de_bh);
  207. if (status < 0) {
  208. mlog_errno(status);
  209. goto leave;
  210. }
  211. /* reserve an inode spot */
  212. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  213. if (status < 0) {
  214. if (status != -ENOSPC)
  215. mlog_errno(status);
  216. goto leave;
  217. }
  218. /* are we making a directory? If so, reserve a cluster for his
  219. * 1st extent. */
  220. if (S_ISDIR(mode)) {
  221. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  222. if (status < 0) {
  223. if (status != -ENOSPC)
  224. mlog_errno(status);
  225. goto leave;
  226. }
  227. }
  228. handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
  229. if (IS_ERR(handle)) {
  230. status = PTR_ERR(handle);
  231. handle = NULL;
  232. mlog_errno(status);
  233. goto leave;
  234. }
  235. /* do the real work now. */
  236. status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
  237. &new_fe_bh, parent_fe_bh, handle,
  238. &inode, inode_ac);
  239. if (status < 0) {
  240. mlog_errno(status);
  241. goto leave;
  242. }
  243. if (S_ISDIR(mode)) {
  244. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  245. new_fe_bh, data_ac);
  246. if (status < 0) {
  247. mlog_errno(status);
  248. goto leave;
  249. }
  250. status = ocfs2_journal_access(handle, dir, parent_fe_bh,
  251. OCFS2_JOURNAL_ACCESS_WRITE);
  252. if (status < 0) {
  253. mlog_errno(status);
  254. goto leave;
  255. }
  256. le16_add_cpu(&dirfe->i_links_count, 1);
  257. status = ocfs2_journal_dirty(handle, parent_fe_bh);
  258. if (status < 0) {
  259. mlog_errno(status);
  260. goto leave;
  261. }
  262. inc_nlink(dir);
  263. }
  264. status = ocfs2_add_entry(handle, dentry, inode,
  265. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  266. de_bh);
  267. if (status < 0) {
  268. mlog_errno(status);
  269. goto leave;
  270. }
  271. status = ocfs2_dentry_attach_lock(dentry, inode,
  272. OCFS2_I(dir)->ip_blkno);
  273. if (status) {
  274. mlog_errno(status);
  275. goto leave;
  276. }
  277. insert_inode_hash(inode);
  278. dentry->d_op = &ocfs2_dentry_ops;
  279. d_instantiate(dentry, inode);
  280. status = 0;
  281. leave:
  282. if (handle)
  283. ocfs2_commit_trans(osb, handle);
  284. ocfs2_meta_unlock(dir, 1);
  285. if (status == -ENOSPC)
  286. mlog(0, "Disk is full\n");
  287. if (new_fe_bh)
  288. brelse(new_fe_bh);
  289. if (de_bh)
  290. brelse(de_bh);
  291. if (parent_fe_bh)
  292. brelse(parent_fe_bh);
  293. if ((status < 0) && inode)
  294. iput(inode);
  295. if (inode_ac)
  296. ocfs2_free_alloc_context(inode_ac);
  297. if (data_ac)
  298. ocfs2_free_alloc_context(data_ac);
  299. mlog_exit(status);
  300. return status;
  301. }
  302. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  303. struct inode *dir,
  304. struct dentry *dentry, int mode,
  305. dev_t dev,
  306. struct buffer_head **new_fe_bh,
  307. struct buffer_head *parent_fe_bh,
  308. handle_t *handle,
  309. struct inode **ret_inode,
  310. struct ocfs2_alloc_context *inode_ac)
  311. {
  312. int status = 0;
  313. struct ocfs2_dinode *fe = NULL;
  314. struct ocfs2_extent_list *fel;
  315. u64 fe_blkno = 0;
  316. u16 suballoc_bit;
  317. struct inode *inode = NULL;
  318. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  319. (unsigned long)dev, dentry->d_name.len,
  320. dentry->d_name.name);
  321. *new_fe_bh = NULL;
  322. *ret_inode = NULL;
  323. status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
  324. &fe_blkno);
  325. if (status < 0) {
  326. mlog_errno(status);
  327. goto leave;
  328. }
  329. inode = new_inode(dir->i_sb);
  330. if (IS_ERR(inode)) {
  331. status = PTR_ERR(inode);
  332. mlog(ML_ERROR, "new_inode failed!\n");
  333. goto leave;
  334. }
  335. /* populate as many fields early on as possible - many of
  336. * these are used by the support functions here and in
  337. * callers. */
  338. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  339. OCFS2_I(inode)->ip_blkno = fe_blkno;
  340. if (S_ISDIR(mode))
  341. inode->i_nlink = 2;
  342. else
  343. inode->i_nlink = 1;
  344. inode->i_mode = mode;
  345. spin_lock(&osb->osb_lock);
  346. inode->i_generation = osb->s_next_generation++;
  347. spin_unlock(&osb->osb_lock);
  348. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  349. if (!*new_fe_bh) {
  350. status = -EIO;
  351. mlog_errno(status);
  352. goto leave;
  353. }
  354. ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
  355. status = ocfs2_journal_access(handle, inode, *new_fe_bh,
  356. OCFS2_JOURNAL_ACCESS_CREATE);
  357. if (status < 0) {
  358. mlog_errno(status);
  359. goto leave;
  360. }
  361. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  362. memset(fe, 0, osb->sb->s_blocksize);
  363. fe->i_generation = cpu_to_le32(inode->i_generation);
  364. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  365. fe->i_blkno = cpu_to_le64(fe_blkno);
  366. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  367. fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
  368. fe->i_uid = cpu_to_le32(current->fsuid);
  369. if (dir->i_mode & S_ISGID) {
  370. fe->i_gid = cpu_to_le32(dir->i_gid);
  371. if (S_ISDIR(mode))
  372. mode |= S_ISGID;
  373. } else
  374. fe->i_gid = cpu_to_le32(current->fsgid);
  375. fe->i_mode = cpu_to_le16(mode);
  376. if (S_ISCHR(mode) || S_ISBLK(mode))
  377. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  378. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  379. fe->i_last_eb_blk = 0;
  380. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  381. le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
  382. fe->i_atime = fe->i_ctime = fe->i_mtime =
  383. cpu_to_le64(CURRENT_TIME.tv_sec);
  384. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  385. cpu_to_le32(CURRENT_TIME.tv_nsec);
  386. fe->i_dtime = 0;
  387. fel = &fe->id2.i_list;
  388. fel->l_tree_depth = 0;
  389. fel->l_next_free_rec = 0;
  390. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  391. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  392. if (status < 0) {
  393. mlog_errno(status);
  394. goto leave;
  395. }
  396. if (ocfs2_populate_inode(inode, fe, 1) < 0) {
  397. mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
  398. "i_blkno=%llu, i_ino=%lu\n",
  399. (unsigned long long)(*new_fe_bh)->b_blocknr,
  400. (unsigned long long)le64_to_cpu(fe->i_blkno),
  401. inode->i_ino);
  402. BUG();
  403. }
  404. ocfs2_inode_set_new(osb, inode);
  405. if (!ocfs2_mount_local(osb)) {
  406. status = ocfs2_create_new_inode_locks(inode);
  407. if (status < 0)
  408. mlog_errno(status);
  409. }
  410. status = 0; /* error in ocfs2_create_new_inode_locks is not
  411. * critical */
  412. *ret_inode = inode;
  413. leave:
  414. if (status < 0) {
  415. if (*new_fe_bh) {
  416. brelse(*new_fe_bh);
  417. *new_fe_bh = NULL;
  418. }
  419. if (inode)
  420. iput(inode);
  421. }
  422. mlog_exit(status);
  423. return status;
  424. }
  425. static int ocfs2_mkdir(struct inode *dir,
  426. struct dentry *dentry,
  427. int mode)
  428. {
  429. int ret;
  430. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  431. dentry->d_name.len, dentry->d_name.name);
  432. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  433. mlog_exit(ret);
  434. return ret;
  435. }
  436. static int ocfs2_create(struct inode *dir,
  437. struct dentry *dentry,
  438. int mode,
  439. struct nameidata *nd)
  440. {
  441. int ret;
  442. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  443. dentry->d_name.len, dentry->d_name.name);
  444. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  445. mlog_exit(ret);
  446. return ret;
  447. }
  448. static int ocfs2_link(struct dentry *old_dentry,
  449. struct inode *dir,
  450. struct dentry *dentry)
  451. {
  452. handle_t *handle;
  453. struct inode *inode = old_dentry->d_inode;
  454. int err;
  455. struct buffer_head *fe_bh = NULL;
  456. struct buffer_head *parent_fe_bh = NULL;
  457. struct buffer_head *de_bh = NULL;
  458. struct ocfs2_dinode *fe = NULL;
  459. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  460. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  461. old_dentry->d_name.len, old_dentry->d_name.name,
  462. dentry->d_name.len, dentry->d_name.name);
  463. if (S_ISDIR(inode->i_mode))
  464. return -EPERM;
  465. err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
  466. if (err < 0) {
  467. if (err != -ENOENT)
  468. mlog_errno(err);
  469. return err;
  470. }
  471. if (!dir->i_nlink) {
  472. err = -ENOENT;
  473. goto out;
  474. }
  475. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  476. dentry->d_name.len);
  477. if (err)
  478. goto out;
  479. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  480. dentry->d_name.name,
  481. dentry->d_name.len, &de_bh);
  482. if (err < 0) {
  483. mlog_errno(err);
  484. goto out;
  485. }
  486. err = ocfs2_meta_lock(inode, &fe_bh, 1);
  487. if (err < 0) {
  488. if (err != -ENOENT)
  489. mlog_errno(err);
  490. goto out;
  491. }
  492. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  493. if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
  494. err = -EMLINK;
  495. goto out_unlock_inode;
  496. }
  497. handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
  498. if (IS_ERR(handle)) {
  499. err = PTR_ERR(handle);
  500. handle = NULL;
  501. mlog_errno(err);
  502. goto out_unlock_inode;
  503. }
  504. err = ocfs2_journal_access(handle, inode, fe_bh,
  505. OCFS2_JOURNAL_ACCESS_WRITE);
  506. if (err < 0) {
  507. mlog_errno(err);
  508. goto out_commit;
  509. }
  510. inc_nlink(inode);
  511. inode->i_ctime = CURRENT_TIME;
  512. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  513. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  514. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  515. err = ocfs2_journal_dirty(handle, fe_bh);
  516. if (err < 0) {
  517. le16_add_cpu(&fe->i_links_count, -1);
  518. drop_nlink(inode);
  519. mlog_errno(err);
  520. goto out_commit;
  521. }
  522. err = ocfs2_add_entry(handle, dentry, inode,
  523. OCFS2_I(inode)->ip_blkno,
  524. parent_fe_bh, de_bh);
  525. if (err) {
  526. le16_add_cpu(&fe->i_links_count, -1);
  527. drop_nlink(inode);
  528. mlog_errno(err);
  529. goto out_commit;
  530. }
  531. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  532. if (err) {
  533. mlog_errno(err);
  534. goto out_commit;
  535. }
  536. atomic_inc(&inode->i_count);
  537. dentry->d_op = &ocfs2_dentry_ops;
  538. d_instantiate(dentry, inode);
  539. out_commit:
  540. ocfs2_commit_trans(osb, handle);
  541. out_unlock_inode:
  542. ocfs2_meta_unlock(inode, 1);
  543. out:
  544. ocfs2_meta_unlock(dir, 1);
  545. if (de_bh)
  546. brelse(de_bh);
  547. if (fe_bh)
  548. brelse(fe_bh);
  549. if (parent_fe_bh)
  550. brelse(parent_fe_bh);
  551. mlog_exit(err);
  552. return err;
  553. }
  554. /*
  555. * Takes and drops an exclusive lock on the given dentry. This will
  556. * force other nodes to drop it.
  557. */
  558. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  559. {
  560. int ret;
  561. ret = ocfs2_dentry_lock(dentry, 1);
  562. if (ret)
  563. mlog_errno(ret);
  564. else
  565. ocfs2_dentry_unlock(dentry, 1);
  566. return ret;
  567. }
  568. static inline int inode_is_unlinkable(struct inode *inode)
  569. {
  570. if (S_ISDIR(inode->i_mode)) {
  571. if (inode->i_nlink == 2)
  572. return 1;
  573. return 0;
  574. }
  575. if (inode->i_nlink == 1)
  576. return 1;
  577. return 0;
  578. }
  579. static int ocfs2_unlink(struct inode *dir,
  580. struct dentry *dentry)
  581. {
  582. int status;
  583. int child_locked = 0;
  584. struct inode *inode = dentry->d_inode;
  585. struct inode *orphan_dir = NULL;
  586. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  587. u64 blkno;
  588. struct ocfs2_dinode *fe = NULL;
  589. struct buffer_head *fe_bh = NULL;
  590. struct buffer_head *parent_node_bh = NULL;
  591. handle_t *handle = NULL;
  592. struct ocfs2_dir_entry *dirent = NULL;
  593. struct buffer_head *dirent_bh = NULL;
  594. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  595. struct buffer_head *orphan_entry_bh = NULL;
  596. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  597. dentry->d_name.len, dentry->d_name.name);
  598. BUG_ON(dentry->d_parent->d_inode != dir);
  599. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  600. if (inode == osb->root_inode) {
  601. mlog(0, "Cannot delete the root directory\n");
  602. return -EPERM;
  603. }
  604. status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
  605. if (status < 0) {
  606. if (status != -ENOENT)
  607. mlog_errno(status);
  608. return status;
  609. }
  610. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  611. dentry->d_name.len, &blkno,
  612. dir, &dirent_bh, &dirent);
  613. if (status < 0) {
  614. if (status != -ENOENT)
  615. mlog_errno(status);
  616. goto leave;
  617. }
  618. if (OCFS2_I(inode)->ip_blkno != blkno) {
  619. status = -ENOENT;
  620. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  621. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  622. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  623. goto leave;
  624. }
  625. status = ocfs2_meta_lock(inode, &fe_bh, 1);
  626. if (status < 0) {
  627. if (status != -ENOENT)
  628. mlog_errno(status);
  629. goto leave;
  630. }
  631. child_locked = 1;
  632. if (S_ISDIR(inode->i_mode)) {
  633. if (!ocfs2_empty_dir(inode)) {
  634. status = -ENOTEMPTY;
  635. goto leave;
  636. } else if (inode->i_nlink != 2) {
  637. status = -ENOTEMPTY;
  638. goto leave;
  639. }
  640. }
  641. status = ocfs2_remote_dentry_delete(dentry);
  642. if (status < 0) {
  643. /* This vote should succeed under all normal
  644. * circumstances. */
  645. mlog_errno(status);
  646. goto leave;
  647. }
  648. if (inode_is_unlinkable(inode)) {
  649. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
  650. orphan_name,
  651. &orphan_entry_bh);
  652. if (status < 0) {
  653. mlog_errno(status);
  654. goto leave;
  655. }
  656. }
  657. handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
  658. if (IS_ERR(handle)) {
  659. status = PTR_ERR(handle);
  660. handle = NULL;
  661. mlog_errno(status);
  662. goto leave;
  663. }
  664. status = ocfs2_journal_access(handle, inode, fe_bh,
  665. OCFS2_JOURNAL_ACCESS_WRITE);
  666. if (status < 0) {
  667. mlog_errno(status);
  668. goto leave;
  669. }
  670. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  671. if (inode_is_unlinkable(inode)) {
  672. status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
  673. orphan_entry_bh, orphan_dir);
  674. if (status < 0) {
  675. mlog_errno(status);
  676. goto leave;
  677. }
  678. }
  679. /* delete the name from the parent dir */
  680. status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
  681. if (status < 0) {
  682. mlog_errno(status);
  683. goto leave;
  684. }
  685. if (S_ISDIR(inode->i_mode))
  686. drop_nlink(inode);
  687. drop_nlink(inode);
  688. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  689. status = ocfs2_journal_dirty(handle, fe_bh);
  690. if (status < 0) {
  691. mlog_errno(status);
  692. goto leave;
  693. }
  694. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  695. if (S_ISDIR(inode->i_mode))
  696. drop_nlink(dir);
  697. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  698. if (status < 0) {
  699. mlog_errno(status);
  700. if (S_ISDIR(inode->i_mode))
  701. inc_nlink(dir);
  702. }
  703. leave:
  704. if (handle)
  705. ocfs2_commit_trans(osb, handle);
  706. if (child_locked)
  707. ocfs2_meta_unlock(inode, 1);
  708. ocfs2_meta_unlock(dir, 1);
  709. if (orphan_dir) {
  710. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  711. ocfs2_meta_unlock(orphan_dir, 1);
  712. mutex_unlock(&orphan_dir->i_mutex);
  713. iput(orphan_dir);
  714. }
  715. if (fe_bh)
  716. brelse(fe_bh);
  717. if (dirent_bh)
  718. brelse(dirent_bh);
  719. if (parent_node_bh)
  720. brelse(parent_node_bh);
  721. if (orphan_entry_bh)
  722. brelse(orphan_entry_bh);
  723. mlog_exit(status);
  724. return status;
  725. }
  726. /*
  727. * The only place this should be used is rename!
  728. * if they have the same id, then the 1st one is the only one locked.
  729. */
  730. static int ocfs2_double_lock(struct ocfs2_super *osb,
  731. struct buffer_head **bh1,
  732. struct inode *inode1,
  733. struct buffer_head **bh2,
  734. struct inode *inode2)
  735. {
  736. int status;
  737. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  738. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  739. struct buffer_head **tmpbh;
  740. struct inode *tmpinode;
  741. mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
  742. (unsigned long long)oi1->ip_blkno,
  743. (unsigned long long)oi2->ip_blkno);
  744. if (*bh1)
  745. *bh1 = NULL;
  746. if (*bh2)
  747. *bh2 = NULL;
  748. /* we always want to lock the one with the lower lockid first. */
  749. if (oi1->ip_blkno != oi2->ip_blkno) {
  750. if (oi1->ip_blkno < oi2->ip_blkno) {
  751. /* switch id1 and id2 around */
  752. mlog(0, "switching them around...\n");
  753. tmpbh = bh2;
  754. bh2 = bh1;
  755. bh1 = tmpbh;
  756. tmpinode = inode2;
  757. inode2 = inode1;
  758. inode1 = tmpinode;
  759. }
  760. /* lock id2 */
  761. status = ocfs2_meta_lock(inode2, bh2, 1);
  762. if (status < 0) {
  763. if (status != -ENOENT)
  764. mlog_errno(status);
  765. goto bail;
  766. }
  767. }
  768. /* lock id1 */
  769. status = ocfs2_meta_lock(inode1, bh1, 1);
  770. if (status < 0) {
  771. /*
  772. * An error return must mean that no cluster locks
  773. * were held on function exit.
  774. */
  775. if (oi1->ip_blkno != oi2->ip_blkno)
  776. ocfs2_meta_unlock(inode2, 1);
  777. if (status != -ENOENT)
  778. mlog_errno(status);
  779. }
  780. bail:
  781. mlog_exit(status);
  782. return status;
  783. }
  784. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  785. {
  786. ocfs2_meta_unlock(inode1, 1);
  787. if (inode1 != inode2)
  788. ocfs2_meta_unlock(inode2, 1);
  789. }
  790. #define PARENT_INO(buffer) \
  791. ((struct ocfs2_dir_entry *) \
  792. ((char *)buffer + \
  793. le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
  794. static int ocfs2_rename(struct inode *old_dir,
  795. struct dentry *old_dentry,
  796. struct inode *new_dir,
  797. struct dentry *new_dentry)
  798. {
  799. int status = 0, rename_lock = 0, parents_locked = 0;
  800. int old_child_locked = 0, new_child_locked = 0;
  801. struct inode *old_inode = old_dentry->d_inode;
  802. struct inode *new_inode = new_dentry->d_inode;
  803. struct inode *orphan_dir = NULL;
  804. struct ocfs2_dinode *newfe = NULL;
  805. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  806. struct buffer_head *orphan_entry_bh = NULL;
  807. struct buffer_head *newfe_bh = NULL;
  808. struct buffer_head *old_inode_bh = NULL;
  809. struct buffer_head *insert_entry_bh = NULL;
  810. struct ocfs2_super *osb = NULL;
  811. u64 newfe_blkno;
  812. handle_t *handle = NULL;
  813. struct buffer_head *old_dir_bh = NULL;
  814. struct buffer_head *new_dir_bh = NULL;
  815. struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
  816. // and new_dentry
  817. struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
  818. struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
  819. // this is the 1st dirent bh
  820. nlink_t old_dir_nlink = old_dir->i_nlink;
  821. struct ocfs2_dinode *old_di;
  822. /* At some point it might be nice to break this function up a
  823. * bit. */
  824. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  825. old_dir, old_dentry, new_dir, new_dentry,
  826. old_dentry->d_name.len, old_dentry->d_name.name,
  827. new_dentry->d_name.len, new_dentry->d_name.name);
  828. osb = OCFS2_SB(old_dir->i_sb);
  829. if (new_inode) {
  830. if (!igrab(new_inode))
  831. BUG();
  832. }
  833. /* Assume a directory hierarchy thusly:
  834. * a/b/c
  835. * a/d
  836. * a,b,c, and d are all directories.
  837. *
  838. * from cwd of 'a' on both nodes:
  839. * node1: mv b/c d
  840. * node2: mv d b/c
  841. *
  842. * And that's why, just like the VFS, we need a file system
  843. * rename lock. */
  844. if (old_dentry != new_dentry) {
  845. status = ocfs2_rename_lock(osb);
  846. if (status < 0) {
  847. mlog_errno(status);
  848. goto bail;
  849. }
  850. rename_lock = 1;
  851. }
  852. /* if old and new are the same, this'll just do one lock. */
  853. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  854. &new_dir_bh, new_dir);
  855. if (status < 0) {
  856. mlog_errno(status);
  857. goto bail;
  858. }
  859. parents_locked = 1;
  860. /* make sure both dirs have bhs
  861. * get an extra ref on old_dir_bh if old==new */
  862. if (!new_dir_bh) {
  863. if (old_dir_bh) {
  864. new_dir_bh = old_dir_bh;
  865. get_bh(new_dir_bh);
  866. } else {
  867. mlog(ML_ERROR, "no old_dir_bh!\n");
  868. status = -EIO;
  869. goto bail;
  870. }
  871. }
  872. /*
  873. * Aside from allowing a meta data update, the locking here
  874. * also ensures that the vote thread on other nodes won't have
  875. * to concurrently downconvert the inode and the dentry locks.
  876. */
  877. status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1);
  878. if (status < 0) {
  879. if (status != -ENOENT)
  880. mlog_errno(status);
  881. goto bail;
  882. }
  883. old_child_locked = 1;
  884. status = ocfs2_remote_dentry_delete(old_dentry);
  885. if (status < 0) {
  886. mlog_errno(status);
  887. goto bail;
  888. }
  889. if (S_ISDIR(old_inode->i_mode)) {
  890. status = -EIO;
  891. old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
  892. if (!old_inode_de_bh)
  893. goto bail;
  894. status = -EIO;
  895. if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
  896. OCFS2_I(old_dir)->ip_blkno)
  897. goto bail;
  898. status = -EMLINK;
  899. if (!new_inode && new_dir!=old_dir &&
  900. new_dir->i_nlink >= OCFS2_LINK_MAX)
  901. goto bail;
  902. }
  903. status = -ENOENT;
  904. old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
  905. old_dentry->d_name.len,
  906. old_dir, &old_de);
  907. if (!old_de_bh)
  908. goto bail;
  909. /*
  910. * Check for inode number is _not_ due to possible IO errors.
  911. * We might rmdir the source, keep it as pwd of some process
  912. * and merrily kill the link to whatever was created under the
  913. * same name. Goodbye sticky bit ;-<
  914. */
  915. if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
  916. goto bail;
  917. /* check if the target already exists (in which case we need
  918. * to delete it */
  919. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  920. new_dentry->d_name.len,
  921. &newfe_blkno, new_dir, &new_de_bh,
  922. &new_de);
  923. /* The only error we allow here is -ENOENT because the new
  924. * file not existing is perfectly valid. */
  925. if ((status < 0) && (status != -ENOENT)) {
  926. /* If we cannot find the file specified we should just */
  927. /* return the error... */
  928. mlog_errno(status);
  929. goto bail;
  930. }
  931. if (!new_de && new_inode)
  932. mlog(ML_ERROR, "inode %lu does not exist in it's parent "
  933. "directory!", new_inode->i_ino);
  934. /* In case we need to overwrite an existing file, we blow it
  935. * away first */
  936. if (new_de) {
  937. /* VFS didn't think there existed an inode here, but
  938. * someone else in the cluster must have raced our
  939. * rename to create one. Today we error cleanly, in
  940. * the future we should consider calling iget to build
  941. * a new struct inode for this entry. */
  942. if (!new_inode) {
  943. status = -EACCES;
  944. mlog(0, "We found an inode for name %.*s but VFS "
  945. "didn't give us one.\n", new_dentry->d_name.len,
  946. new_dentry->d_name.name);
  947. goto bail;
  948. }
  949. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  950. status = -EACCES;
  951. mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
  952. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  953. (unsigned long long)newfe_blkno,
  954. OCFS2_I(new_inode)->ip_flags);
  955. goto bail;
  956. }
  957. status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
  958. if (status < 0) {
  959. if (status != -ENOENT)
  960. mlog_errno(status);
  961. goto bail;
  962. }
  963. new_child_locked = 1;
  964. status = ocfs2_remote_dentry_delete(new_dentry);
  965. if (status < 0) {
  966. mlog_errno(status);
  967. goto bail;
  968. }
  969. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  970. mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
  971. "newfebh=%p bhblocknr=%llu\n", new_de,
  972. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  973. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  974. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  975. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  976. new_inode,
  977. orphan_name,
  978. &orphan_entry_bh);
  979. if (status < 0) {
  980. mlog_errno(status);
  981. goto bail;
  982. }
  983. }
  984. } else {
  985. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  986. status = ocfs2_check_dir_for_entry(new_dir,
  987. new_dentry->d_name.name,
  988. new_dentry->d_name.len);
  989. if (status)
  990. goto bail;
  991. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  992. new_dentry->d_name.name,
  993. new_dentry->d_name.len,
  994. &insert_entry_bh);
  995. if (status < 0) {
  996. mlog_errno(status);
  997. goto bail;
  998. }
  999. }
  1000. handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
  1001. if (IS_ERR(handle)) {
  1002. status = PTR_ERR(handle);
  1003. handle = NULL;
  1004. mlog_errno(status);
  1005. goto bail;
  1006. }
  1007. if (new_de) {
  1008. if (S_ISDIR(new_inode->i_mode)) {
  1009. if (!ocfs2_empty_dir(new_inode) ||
  1010. new_inode->i_nlink != 2) {
  1011. status = -ENOTEMPTY;
  1012. goto bail;
  1013. }
  1014. }
  1015. status = ocfs2_journal_access(handle, new_inode, newfe_bh,
  1016. OCFS2_JOURNAL_ACCESS_WRITE);
  1017. if (status < 0) {
  1018. mlog_errno(status);
  1019. goto bail;
  1020. }
  1021. if (S_ISDIR(new_inode->i_mode) ||
  1022. (newfe->i_links_count == cpu_to_le16(1))){
  1023. status = ocfs2_orphan_add(osb, handle, new_inode,
  1024. newfe, orphan_name,
  1025. orphan_entry_bh, orphan_dir);
  1026. if (status < 0) {
  1027. mlog_errno(status);
  1028. goto bail;
  1029. }
  1030. }
  1031. /* change the dirent to point to the correct inode */
  1032. status = ocfs2_journal_access(handle, new_dir, new_de_bh,
  1033. OCFS2_JOURNAL_ACCESS_WRITE);
  1034. if (status < 0) {
  1035. mlog_errno(status);
  1036. goto bail;
  1037. }
  1038. new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
  1039. new_de->file_type = old_de->file_type;
  1040. new_dir->i_version++;
  1041. status = ocfs2_journal_dirty(handle, new_de_bh);
  1042. if (status < 0) {
  1043. mlog_errno(status);
  1044. goto bail;
  1045. }
  1046. if (S_ISDIR(new_inode->i_mode))
  1047. newfe->i_links_count = 0;
  1048. else
  1049. le16_add_cpu(&newfe->i_links_count, -1);
  1050. status = ocfs2_journal_dirty(handle, newfe_bh);
  1051. if (status < 0) {
  1052. mlog_errno(status);
  1053. goto bail;
  1054. }
  1055. } else {
  1056. /* if the name was not found in new_dir, add it now */
  1057. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1058. OCFS2_I(old_inode)->ip_blkno,
  1059. new_dir_bh, insert_entry_bh);
  1060. }
  1061. old_inode->i_ctime = CURRENT_TIME;
  1062. mark_inode_dirty(old_inode);
  1063. status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
  1064. OCFS2_JOURNAL_ACCESS_WRITE);
  1065. if (status >= 0) {
  1066. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1067. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1068. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1069. status = ocfs2_journal_dirty(handle, old_inode_bh);
  1070. if (status < 0)
  1071. mlog_errno(status);
  1072. } else
  1073. mlog_errno(status);
  1074. /* now that the name has been added to new_dir, remove the old name */
  1075. status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
  1076. if (status < 0) {
  1077. mlog_errno(status);
  1078. goto bail;
  1079. }
  1080. if (new_inode) {
  1081. new_inode->i_nlink--;
  1082. new_inode->i_ctime = CURRENT_TIME;
  1083. }
  1084. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1085. if (old_inode_de_bh) {
  1086. status = ocfs2_journal_access(handle, old_inode,
  1087. old_inode_de_bh,
  1088. OCFS2_JOURNAL_ACCESS_WRITE);
  1089. PARENT_INO(old_inode_de_bh->b_data) =
  1090. cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
  1091. status = ocfs2_journal_dirty(handle, old_inode_de_bh);
  1092. old_dir->i_nlink--;
  1093. if (new_inode) {
  1094. new_inode->i_nlink--;
  1095. } else {
  1096. inc_nlink(new_dir);
  1097. mark_inode_dirty(new_dir);
  1098. }
  1099. }
  1100. mark_inode_dirty(old_dir);
  1101. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1102. if (new_inode) {
  1103. mark_inode_dirty(new_inode);
  1104. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1105. }
  1106. if (old_dir != new_dir) {
  1107. /* Keep the same times on both directories.*/
  1108. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1109. /*
  1110. * This will also pick up the i_nlink change from the
  1111. * block above.
  1112. */
  1113. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1114. }
  1115. if (old_dir_nlink != old_dir->i_nlink) {
  1116. if (!old_dir_bh) {
  1117. mlog(ML_ERROR, "need to change nlink for old dir "
  1118. "%llu from %d to %d but bh is NULL!\n",
  1119. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1120. (int)old_dir_nlink, old_dir->i_nlink);
  1121. } else {
  1122. struct ocfs2_dinode *fe;
  1123. status = ocfs2_journal_access(handle, old_dir,
  1124. old_dir_bh,
  1125. OCFS2_JOURNAL_ACCESS_WRITE);
  1126. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1127. fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
  1128. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1129. }
  1130. }
  1131. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1132. status = 0;
  1133. bail:
  1134. if (rename_lock)
  1135. ocfs2_rename_unlock(osb);
  1136. if (handle)
  1137. ocfs2_commit_trans(osb, handle);
  1138. if (parents_locked)
  1139. ocfs2_double_unlock(old_dir, new_dir);
  1140. if (old_child_locked)
  1141. ocfs2_meta_unlock(old_inode, 1);
  1142. if (new_child_locked)
  1143. ocfs2_meta_unlock(new_inode, 1);
  1144. if (orphan_dir) {
  1145. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1146. ocfs2_meta_unlock(orphan_dir, 1);
  1147. mutex_unlock(&orphan_dir->i_mutex);
  1148. iput(orphan_dir);
  1149. }
  1150. if (new_inode)
  1151. sync_mapping_buffers(old_inode->i_mapping);
  1152. if (new_inode)
  1153. iput(new_inode);
  1154. if (newfe_bh)
  1155. brelse(newfe_bh);
  1156. if (old_inode_bh)
  1157. brelse(old_inode_bh);
  1158. if (old_dir_bh)
  1159. brelse(old_dir_bh);
  1160. if (new_dir_bh)
  1161. brelse(new_dir_bh);
  1162. if (new_de_bh)
  1163. brelse(new_de_bh);
  1164. if (old_de_bh)
  1165. brelse(old_de_bh);
  1166. if (old_inode_de_bh)
  1167. brelse(old_inode_de_bh);
  1168. if (orphan_entry_bh)
  1169. brelse(orphan_entry_bh);
  1170. if (insert_entry_bh)
  1171. brelse(insert_entry_bh);
  1172. mlog_exit(status);
  1173. return status;
  1174. }
  1175. /*
  1176. * we expect i_size = strlen(symname). Copy symname into the file
  1177. * data, including the null terminator.
  1178. */
  1179. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1180. handle_t *handle,
  1181. struct inode *inode,
  1182. const char *symname)
  1183. {
  1184. struct buffer_head **bhs = NULL;
  1185. const char *c;
  1186. struct super_block *sb = osb->sb;
  1187. u64 p_blkno, p_blocks;
  1188. int virtual, blocks, status, i, bytes_left;
  1189. bytes_left = i_size_read(inode) + 1;
  1190. /* we can't trust i_blocks because we're actually going to
  1191. * write i_size + 1 bytes. */
  1192. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1193. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1194. (unsigned long long)inode->i_blocks,
  1195. i_size_read(inode), blocks);
  1196. /* Sanity check -- make sure we're going to fit. */
  1197. if (bytes_left >
  1198. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1199. status = -EIO;
  1200. mlog_errno(status);
  1201. goto bail;
  1202. }
  1203. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1204. if (!bhs) {
  1205. status = -ENOMEM;
  1206. mlog_errno(status);
  1207. goto bail;
  1208. }
  1209. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1210. NULL);
  1211. if (status < 0) {
  1212. mlog_errno(status);
  1213. goto bail;
  1214. }
  1215. /* links can never be larger than one cluster so we know this
  1216. * is all going to be contiguous, but do a sanity check
  1217. * anyway. */
  1218. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1219. status = -EIO;
  1220. mlog_errno(status);
  1221. goto bail;
  1222. }
  1223. virtual = 0;
  1224. while(bytes_left > 0) {
  1225. c = &symname[virtual * sb->s_blocksize];
  1226. bhs[virtual] = sb_getblk(sb, p_blkno);
  1227. if (!bhs[virtual]) {
  1228. status = -ENOMEM;
  1229. mlog_errno(status);
  1230. goto bail;
  1231. }
  1232. ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
  1233. status = ocfs2_journal_access(handle, inode, bhs[virtual],
  1234. OCFS2_JOURNAL_ACCESS_CREATE);
  1235. if (status < 0) {
  1236. mlog_errno(status);
  1237. goto bail;
  1238. }
  1239. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1240. memcpy(bhs[virtual]->b_data, c,
  1241. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1242. bytes_left);
  1243. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1244. if (status < 0) {
  1245. mlog_errno(status);
  1246. goto bail;
  1247. }
  1248. virtual++;
  1249. p_blkno++;
  1250. bytes_left -= sb->s_blocksize;
  1251. }
  1252. status = 0;
  1253. bail:
  1254. if (bhs) {
  1255. for(i = 0; i < blocks; i++)
  1256. if (bhs[i])
  1257. brelse(bhs[i]);
  1258. kfree(bhs);
  1259. }
  1260. mlog_exit(status);
  1261. return status;
  1262. }
  1263. static int ocfs2_symlink(struct inode *dir,
  1264. struct dentry *dentry,
  1265. const char *symname)
  1266. {
  1267. int status, l, credits;
  1268. u64 newsize;
  1269. struct ocfs2_super *osb = NULL;
  1270. struct inode *inode = NULL;
  1271. struct super_block *sb;
  1272. struct buffer_head *new_fe_bh = NULL;
  1273. struct buffer_head *de_bh = NULL;
  1274. struct buffer_head *parent_fe_bh = NULL;
  1275. struct ocfs2_dinode *fe = NULL;
  1276. struct ocfs2_dinode *dirfe;
  1277. handle_t *handle = NULL;
  1278. struct ocfs2_alloc_context *inode_ac = NULL;
  1279. struct ocfs2_alloc_context *data_ac = NULL;
  1280. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1281. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1282. sb = dir->i_sb;
  1283. osb = OCFS2_SB(sb);
  1284. l = strlen(symname) + 1;
  1285. credits = ocfs2_calc_symlink_credits(sb);
  1286. /* lock the parent directory */
  1287. status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
  1288. if (status < 0) {
  1289. if (status != -ENOENT)
  1290. mlog_errno(status);
  1291. return status;
  1292. }
  1293. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1294. if (!dirfe->i_links_count) {
  1295. /* can't make a file in a deleted directory. */
  1296. status = -ENOENT;
  1297. goto bail;
  1298. }
  1299. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1300. dentry->d_name.len);
  1301. if (status)
  1302. goto bail;
  1303. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1304. dentry->d_name.name,
  1305. dentry->d_name.len, &de_bh);
  1306. if (status < 0) {
  1307. mlog_errno(status);
  1308. goto bail;
  1309. }
  1310. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1311. if (status < 0) {
  1312. if (status != -ENOSPC)
  1313. mlog_errno(status);
  1314. goto bail;
  1315. }
  1316. /* don't reserve bitmap space for fast symlinks. */
  1317. if (l > ocfs2_fast_symlink_chars(sb)) {
  1318. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  1319. if (status < 0) {
  1320. if (status != -ENOSPC)
  1321. mlog_errno(status);
  1322. goto bail;
  1323. }
  1324. }
  1325. handle = ocfs2_start_trans(osb, credits);
  1326. if (IS_ERR(handle)) {
  1327. status = PTR_ERR(handle);
  1328. handle = NULL;
  1329. mlog_errno(status);
  1330. goto bail;
  1331. }
  1332. status = ocfs2_mknod_locked(osb, dir, dentry,
  1333. S_IFLNK | S_IRWXUGO, 0,
  1334. &new_fe_bh, parent_fe_bh, handle,
  1335. &inode, inode_ac);
  1336. if (status < 0) {
  1337. mlog_errno(status);
  1338. goto bail;
  1339. }
  1340. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1341. inode->i_rdev = 0;
  1342. newsize = l - 1;
  1343. if (l > ocfs2_fast_symlink_chars(sb)) {
  1344. u32 offset = 0;
  1345. inode->i_op = &ocfs2_symlink_inode_operations;
  1346. status = ocfs2_do_extend_allocation(osb, inode, &offset, 1, 0,
  1347. new_fe_bh,
  1348. handle, data_ac, NULL,
  1349. NULL);
  1350. if (status < 0) {
  1351. if (status != -ENOSPC && status != -EINTR) {
  1352. mlog(ML_ERROR,
  1353. "Failed to extend file to %llu\n",
  1354. (unsigned long long)newsize);
  1355. mlog_errno(status);
  1356. status = -ENOSPC;
  1357. }
  1358. goto bail;
  1359. }
  1360. i_size_write(inode, newsize);
  1361. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1362. } else {
  1363. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1364. memcpy((char *) fe->id2.i_symlink, symname, l);
  1365. i_size_write(inode, newsize);
  1366. inode->i_blocks = 0;
  1367. }
  1368. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1369. if (status < 0) {
  1370. mlog_errno(status);
  1371. goto bail;
  1372. }
  1373. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1374. status = ocfs2_create_symlink_data(osb, handle, inode,
  1375. symname);
  1376. if (status < 0) {
  1377. mlog_errno(status);
  1378. goto bail;
  1379. }
  1380. }
  1381. status = ocfs2_add_entry(handle, dentry, inode,
  1382. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1383. de_bh);
  1384. if (status < 0) {
  1385. mlog_errno(status);
  1386. goto bail;
  1387. }
  1388. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1389. if (status) {
  1390. mlog_errno(status);
  1391. goto bail;
  1392. }
  1393. insert_inode_hash(inode);
  1394. dentry->d_op = &ocfs2_dentry_ops;
  1395. d_instantiate(dentry, inode);
  1396. bail:
  1397. if (handle)
  1398. ocfs2_commit_trans(osb, handle);
  1399. ocfs2_meta_unlock(dir, 1);
  1400. if (new_fe_bh)
  1401. brelse(new_fe_bh);
  1402. if (parent_fe_bh)
  1403. brelse(parent_fe_bh);
  1404. if (de_bh)
  1405. brelse(de_bh);
  1406. if (inode_ac)
  1407. ocfs2_free_alloc_context(inode_ac);
  1408. if (data_ac)
  1409. ocfs2_free_alloc_context(data_ac);
  1410. if ((status < 0) && inode)
  1411. iput(inode);
  1412. mlog_exit(status);
  1413. return status;
  1414. }
  1415. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1416. {
  1417. int status, namelen;
  1418. mlog_entry_void();
  1419. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1420. (long long)blkno);
  1421. if (namelen <= 0) {
  1422. if (namelen)
  1423. status = namelen;
  1424. else
  1425. status = -EINVAL;
  1426. mlog_errno(status);
  1427. goto bail;
  1428. }
  1429. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1430. status = -EINVAL;
  1431. mlog_errno(status);
  1432. goto bail;
  1433. }
  1434. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1435. namelen);
  1436. status = 0;
  1437. bail:
  1438. mlog_exit(status);
  1439. return status;
  1440. }
  1441. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1442. struct inode **ret_orphan_dir,
  1443. struct inode *inode,
  1444. char *name,
  1445. struct buffer_head **de_bh)
  1446. {
  1447. struct inode *orphan_dir_inode;
  1448. struct buffer_head *orphan_dir_bh = NULL;
  1449. int status = 0;
  1450. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1451. if (status < 0) {
  1452. mlog_errno(status);
  1453. return status;
  1454. }
  1455. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1456. ORPHAN_DIR_SYSTEM_INODE,
  1457. osb->slot_num);
  1458. if (!orphan_dir_inode) {
  1459. status = -ENOENT;
  1460. mlog_errno(status);
  1461. return status;
  1462. }
  1463. mutex_lock(&orphan_dir_inode->i_mutex);
  1464. status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1465. if (status < 0) {
  1466. mlog_errno(status);
  1467. goto leave;
  1468. }
  1469. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1470. orphan_dir_bh, name,
  1471. OCFS2_ORPHAN_NAMELEN, de_bh);
  1472. if (status < 0) {
  1473. ocfs2_meta_unlock(orphan_dir_inode, 1);
  1474. mlog_errno(status);
  1475. goto leave;
  1476. }
  1477. *ret_orphan_dir = orphan_dir_inode;
  1478. leave:
  1479. if (status) {
  1480. mutex_unlock(&orphan_dir_inode->i_mutex);
  1481. iput(orphan_dir_inode);
  1482. }
  1483. if (orphan_dir_bh)
  1484. brelse(orphan_dir_bh);
  1485. mlog_exit(status);
  1486. return status;
  1487. }
  1488. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1489. handle_t *handle,
  1490. struct inode *inode,
  1491. struct ocfs2_dinode *fe,
  1492. char *name,
  1493. struct buffer_head *de_bh,
  1494. struct inode *orphan_dir_inode)
  1495. {
  1496. struct buffer_head *orphan_dir_bh = NULL;
  1497. int status = 0;
  1498. struct ocfs2_dinode *orphan_fe;
  1499. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  1500. status = ocfs2_read_block(osb,
  1501. OCFS2_I(orphan_dir_inode)->ip_blkno,
  1502. &orphan_dir_bh, OCFS2_BH_CACHED,
  1503. orphan_dir_inode);
  1504. if (status < 0) {
  1505. mlog_errno(status);
  1506. goto leave;
  1507. }
  1508. status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
  1509. OCFS2_JOURNAL_ACCESS_WRITE);
  1510. if (status < 0) {
  1511. mlog_errno(status);
  1512. goto leave;
  1513. }
  1514. /* we're a cluster, and nlink can change on disk from
  1515. * underneath us... */
  1516. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1517. if (S_ISDIR(inode->i_mode))
  1518. le16_add_cpu(&orphan_fe->i_links_count, 1);
  1519. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1520. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1521. if (status < 0) {
  1522. mlog_errno(status);
  1523. goto leave;
  1524. }
  1525. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1526. OCFS2_ORPHAN_NAMELEN, inode,
  1527. OCFS2_I(inode)->ip_blkno,
  1528. orphan_dir_bh, de_bh);
  1529. if (status < 0) {
  1530. mlog_errno(status);
  1531. goto leave;
  1532. }
  1533. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1534. /* Record which orphan dir our inode now resides
  1535. * in. delete_inode will use this to determine which orphan
  1536. * dir to lock. */
  1537. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1538. mlog(0, "Inode %llu orphaned in slot %d\n",
  1539. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1540. leave:
  1541. if (orphan_dir_bh)
  1542. brelse(orphan_dir_bh);
  1543. mlog_exit(status);
  1544. return status;
  1545. }
  1546. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1547. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1548. handle_t *handle,
  1549. struct inode *orphan_dir_inode,
  1550. struct inode *inode,
  1551. struct buffer_head *orphan_dir_bh)
  1552. {
  1553. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1554. struct ocfs2_dinode *orphan_fe;
  1555. int status = 0;
  1556. struct buffer_head *target_de_bh = NULL;
  1557. struct ocfs2_dir_entry *target_de = NULL;
  1558. mlog_entry_void();
  1559. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1560. if (status < 0) {
  1561. mlog_errno(status);
  1562. goto leave;
  1563. }
  1564. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1565. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1566. OCFS2_ORPHAN_NAMELEN);
  1567. /* find it's spot in the orphan directory */
  1568. target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
  1569. orphan_dir_inode, &target_de);
  1570. if (!target_de_bh) {
  1571. status = -ENOENT;
  1572. mlog_errno(status);
  1573. goto leave;
  1574. }
  1575. /* remove it from the orphan directory */
  1576. status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
  1577. target_de_bh);
  1578. if (status < 0) {
  1579. mlog_errno(status);
  1580. goto leave;
  1581. }
  1582. status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
  1583. OCFS2_JOURNAL_ACCESS_WRITE);
  1584. if (status < 0) {
  1585. mlog_errno(status);
  1586. goto leave;
  1587. }
  1588. /* do the i_nlink dance! :) */
  1589. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1590. if (S_ISDIR(inode->i_mode))
  1591. le16_add_cpu(&orphan_fe->i_links_count, -1);
  1592. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1593. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1594. if (status < 0) {
  1595. mlog_errno(status);
  1596. goto leave;
  1597. }
  1598. leave:
  1599. if (target_de_bh)
  1600. brelse(target_de_bh);
  1601. mlog_exit(status);
  1602. return status;
  1603. }
  1604. const struct inode_operations ocfs2_dir_iops = {
  1605. .create = ocfs2_create,
  1606. .lookup = ocfs2_lookup,
  1607. .link = ocfs2_link,
  1608. .unlink = ocfs2_unlink,
  1609. .rmdir = ocfs2_unlink,
  1610. .symlink = ocfs2_symlink,
  1611. .mkdir = ocfs2_mkdir,
  1612. .mknod = ocfs2_mknod,
  1613. .rename = ocfs2_rename,
  1614. .setattr = ocfs2_setattr,
  1615. .getattr = ocfs2_getattr,
  1616. .permission = ocfs2_permission,
  1617. };