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 "xattr.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_inode_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, 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 downconvert thread and have a stale dentry. */
  158. ocfs2_inode_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_inode_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. /* Reserve a cluster if creating an extent based directory. */
  219. if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
  220. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  221. if (status < 0) {
  222. if (status != -ENOSPC)
  223. mlog_errno(status);
  224. goto leave;
  225. }
  226. }
  227. handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
  228. if (IS_ERR(handle)) {
  229. status = PTR_ERR(handle);
  230. handle = NULL;
  231. mlog_errno(status);
  232. goto leave;
  233. }
  234. /* do the real work now. */
  235. status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
  236. &new_fe_bh, parent_fe_bh, handle,
  237. &inode, inode_ac);
  238. if (status < 0) {
  239. mlog_errno(status);
  240. goto leave;
  241. }
  242. if (S_ISDIR(mode)) {
  243. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  244. new_fe_bh, data_ac);
  245. if (status < 0) {
  246. mlog_errno(status);
  247. goto leave;
  248. }
  249. status = ocfs2_journal_access(handle, dir, parent_fe_bh,
  250. OCFS2_JOURNAL_ACCESS_WRITE);
  251. if (status < 0) {
  252. mlog_errno(status);
  253. goto leave;
  254. }
  255. le16_add_cpu(&dirfe->i_links_count, 1);
  256. status = ocfs2_journal_dirty(handle, parent_fe_bh);
  257. if (status < 0) {
  258. mlog_errno(status);
  259. goto leave;
  260. }
  261. inc_nlink(dir);
  262. }
  263. status = ocfs2_add_entry(handle, dentry, inode,
  264. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  265. de_bh);
  266. if (status < 0) {
  267. mlog_errno(status);
  268. goto leave;
  269. }
  270. status = ocfs2_dentry_attach_lock(dentry, inode,
  271. OCFS2_I(dir)->ip_blkno);
  272. if (status) {
  273. mlog_errno(status);
  274. goto leave;
  275. }
  276. insert_inode_hash(inode);
  277. dentry->d_op = &ocfs2_dentry_ops;
  278. d_instantiate(dentry, inode);
  279. status = 0;
  280. leave:
  281. if (handle)
  282. ocfs2_commit_trans(osb, handle);
  283. ocfs2_inode_unlock(dir, 1);
  284. if (status == -ENOSPC)
  285. mlog(0, "Disk is full\n");
  286. brelse(new_fe_bh);
  287. brelse(de_bh);
  288. brelse(parent_fe_bh);
  289. if ((status < 0) && inode)
  290. iput(inode);
  291. if (inode_ac)
  292. ocfs2_free_alloc_context(inode_ac);
  293. if (data_ac)
  294. ocfs2_free_alloc_context(data_ac);
  295. mlog_exit(status);
  296. return status;
  297. }
  298. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  299. struct inode *dir,
  300. struct dentry *dentry, int mode,
  301. dev_t dev,
  302. struct buffer_head **new_fe_bh,
  303. struct buffer_head *parent_fe_bh,
  304. handle_t *handle,
  305. struct inode **ret_inode,
  306. struct ocfs2_alloc_context *inode_ac)
  307. {
  308. int status = 0;
  309. struct ocfs2_dinode *fe = NULL;
  310. struct ocfs2_extent_list *fel;
  311. u64 fe_blkno = 0;
  312. u16 suballoc_bit;
  313. struct inode *inode = NULL;
  314. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  315. (unsigned long)dev, dentry->d_name.len,
  316. dentry->d_name.name);
  317. *new_fe_bh = NULL;
  318. *ret_inode = NULL;
  319. status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
  320. &fe_blkno);
  321. if (status < 0) {
  322. mlog_errno(status);
  323. goto leave;
  324. }
  325. inode = new_inode(dir->i_sb);
  326. if (!inode) {
  327. status = -ENOMEM;
  328. mlog(ML_ERROR, "new_inode failed!\n");
  329. goto leave;
  330. }
  331. /* populate as many fields early on as possible - many of
  332. * these are used by the support functions here and in
  333. * callers. */
  334. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  335. OCFS2_I(inode)->ip_blkno = fe_blkno;
  336. if (S_ISDIR(mode))
  337. inode->i_nlink = 2;
  338. else
  339. inode->i_nlink = 1;
  340. inode->i_mode = mode;
  341. spin_lock(&osb->osb_lock);
  342. inode->i_generation = osb->s_next_generation++;
  343. spin_unlock(&osb->osb_lock);
  344. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  345. if (!*new_fe_bh) {
  346. status = -EIO;
  347. mlog_errno(status);
  348. goto leave;
  349. }
  350. ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
  351. status = ocfs2_journal_access(handle, inode, *new_fe_bh,
  352. OCFS2_JOURNAL_ACCESS_CREATE);
  353. if (status < 0) {
  354. mlog_errno(status);
  355. goto leave;
  356. }
  357. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  358. memset(fe, 0, osb->sb->s_blocksize);
  359. fe->i_generation = cpu_to_le32(inode->i_generation);
  360. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  361. fe->i_blkno = cpu_to_le64(fe_blkno);
  362. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  363. fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
  364. fe->i_uid = cpu_to_le32(current->fsuid);
  365. if (dir->i_mode & S_ISGID) {
  366. fe->i_gid = cpu_to_le32(dir->i_gid);
  367. if (S_ISDIR(mode))
  368. mode |= S_ISGID;
  369. } else
  370. fe->i_gid = cpu_to_le32(current->fsgid);
  371. fe->i_mode = cpu_to_le16(mode);
  372. if (S_ISCHR(mode) || S_ISBLK(mode))
  373. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  374. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  375. fe->i_last_eb_blk = 0;
  376. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  377. le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
  378. fe->i_atime = fe->i_ctime = fe->i_mtime =
  379. cpu_to_le64(CURRENT_TIME.tv_sec);
  380. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  381. cpu_to_le32(CURRENT_TIME.tv_nsec);
  382. fe->i_dtime = 0;
  383. /*
  384. * If supported, directories start with inline data.
  385. */
  386. if (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) {
  387. u16 feat = le16_to_cpu(fe->i_dyn_features);
  388. fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
  389. fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
  390. } else {
  391. fel = &fe->id2.i_list;
  392. fel->l_tree_depth = 0;
  393. fel->l_next_free_rec = 0;
  394. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  395. }
  396. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  397. if (status < 0) {
  398. mlog_errno(status);
  399. goto leave;
  400. }
  401. if (ocfs2_populate_inode(inode, fe, 1) < 0) {
  402. mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
  403. "i_blkno=%llu, i_ino=%lu\n",
  404. (unsigned long long)(*new_fe_bh)->b_blocknr,
  405. (unsigned long long)le64_to_cpu(fe->i_blkno),
  406. inode->i_ino);
  407. BUG();
  408. }
  409. ocfs2_inode_set_new(osb, inode);
  410. if (!ocfs2_mount_local(osb)) {
  411. status = ocfs2_create_new_inode_locks(inode);
  412. if (status < 0)
  413. mlog_errno(status);
  414. }
  415. status = 0; /* error in ocfs2_create_new_inode_locks is not
  416. * critical */
  417. *ret_inode = inode;
  418. leave:
  419. if (status < 0) {
  420. if (*new_fe_bh) {
  421. brelse(*new_fe_bh);
  422. *new_fe_bh = NULL;
  423. }
  424. if (inode) {
  425. clear_nlink(inode);
  426. iput(inode);
  427. }
  428. }
  429. mlog_exit(status);
  430. return status;
  431. }
  432. static int ocfs2_mkdir(struct inode *dir,
  433. struct dentry *dentry,
  434. int mode)
  435. {
  436. int ret;
  437. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  438. dentry->d_name.len, dentry->d_name.name);
  439. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  440. mlog_exit(ret);
  441. return ret;
  442. }
  443. static int ocfs2_create(struct inode *dir,
  444. struct dentry *dentry,
  445. int mode,
  446. struct nameidata *nd)
  447. {
  448. int ret;
  449. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  450. dentry->d_name.len, dentry->d_name.name);
  451. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  452. mlog_exit(ret);
  453. return ret;
  454. }
  455. static int ocfs2_link(struct dentry *old_dentry,
  456. struct inode *dir,
  457. struct dentry *dentry)
  458. {
  459. handle_t *handle;
  460. struct inode *inode = old_dentry->d_inode;
  461. int err;
  462. struct buffer_head *fe_bh = NULL;
  463. struct buffer_head *parent_fe_bh = NULL;
  464. struct buffer_head *de_bh = NULL;
  465. struct ocfs2_dinode *fe = NULL;
  466. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  467. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  468. old_dentry->d_name.len, old_dentry->d_name.name,
  469. dentry->d_name.len, dentry->d_name.name);
  470. if (S_ISDIR(inode->i_mode))
  471. return -EPERM;
  472. err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  473. if (err < 0) {
  474. if (err != -ENOENT)
  475. mlog_errno(err);
  476. return err;
  477. }
  478. if (!dir->i_nlink) {
  479. err = -ENOENT;
  480. goto out;
  481. }
  482. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  483. dentry->d_name.len);
  484. if (err)
  485. goto out;
  486. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  487. dentry->d_name.name,
  488. dentry->d_name.len, &de_bh);
  489. if (err < 0) {
  490. mlog_errno(err);
  491. goto out;
  492. }
  493. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  494. if (err < 0) {
  495. if (err != -ENOENT)
  496. mlog_errno(err);
  497. goto out;
  498. }
  499. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  500. if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
  501. err = -EMLINK;
  502. goto out_unlock_inode;
  503. }
  504. handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
  505. if (IS_ERR(handle)) {
  506. err = PTR_ERR(handle);
  507. handle = NULL;
  508. mlog_errno(err);
  509. goto out_unlock_inode;
  510. }
  511. err = ocfs2_journal_access(handle, inode, fe_bh,
  512. OCFS2_JOURNAL_ACCESS_WRITE);
  513. if (err < 0) {
  514. mlog_errno(err);
  515. goto out_commit;
  516. }
  517. inc_nlink(inode);
  518. inode->i_ctime = CURRENT_TIME;
  519. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  520. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  521. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  522. err = ocfs2_journal_dirty(handle, fe_bh);
  523. if (err < 0) {
  524. le16_add_cpu(&fe->i_links_count, -1);
  525. drop_nlink(inode);
  526. mlog_errno(err);
  527. goto out_commit;
  528. }
  529. err = ocfs2_add_entry(handle, dentry, inode,
  530. OCFS2_I(inode)->ip_blkno,
  531. parent_fe_bh, de_bh);
  532. if (err) {
  533. le16_add_cpu(&fe->i_links_count, -1);
  534. drop_nlink(inode);
  535. mlog_errno(err);
  536. goto out_commit;
  537. }
  538. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  539. if (err) {
  540. mlog_errno(err);
  541. goto out_commit;
  542. }
  543. atomic_inc(&inode->i_count);
  544. dentry->d_op = &ocfs2_dentry_ops;
  545. d_instantiate(dentry, inode);
  546. out_commit:
  547. ocfs2_commit_trans(osb, handle);
  548. out_unlock_inode:
  549. ocfs2_inode_unlock(inode, 1);
  550. out:
  551. ocfs2_inode_unlock(dir, 1);
  552. brelse(de_bh);
  553. brelse(fe_bh);
  554. brelse(parent_fe_bh);
  555. mlog_exit(err);
  556. return err;
  557. }
  558. /*
  559. * Takes and drops an exclusive lock on the given dentry. This will
  560. * force other nodes to drop it.
  561. */
  562. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  563. {
  564. int ret;
  565. ret = ocfs2_dentry_lock(dentry, 1);
  566. if (ret)
  567. mlog_errno(ret);
  568. else
  569. ocfs2_dentry_unlock(dentry, 1);
  570. return ret;
  571. }
  572. static inline int inode_is_unlinkable(struct inode *inode)
  573. {
  574. if (S_ISDIR(inode->i_mode)) {
  575. if (inode->i_nlink == 2)
  576. return 1;
  577. return 0;
  578. }
  579. if (inode->i_nlink == 1)
  580. return 1;
  581. return 0;
  582. }
  583. static int ocfs2_unlink(struct inode *dir,
  584. struct dentry *dentry)
  585. {
  586. int status;
  587. int child_locked = 0;
  588. struct inode *inode = dentry->d_inode;
  589. struct inode *orphan_dir = NULL;
  590. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  591. u64 blkno;
  592. struct ocfs2_dinode *fe = NULL;
  593. struct buffer_head *fe_bh = NULL;
  594. struct buffer_head *parent_node_bh = NULL;
  595. handle_t *handle = NULL;
  596. struct ocfs2_dir_entry *dirent = NULL;
  597. struct buffer_head *dirent_bh = NULL;
  598. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  599. struct buffer_head *orphan_entry_bh = NULL;
  600. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  601. dentry->d_name.len, dentry->d_name.name);
  602. BUG_ON(dentry->d_parent->d_inode != dir);
  603. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  604. if (inode == osb->root_inode) {
  605. mlog(0, "Cannot delete the root directory\n");
  606. return -EPERM;
  607. }
  608. status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
  609. if (status < 0) {
  610. if (status != -ENOENT)
  611. mlog_errno(status);
  612. return status;
  613. }
  614. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  615. dentry->d_name.len, &blkno,
  616. dir, &dirent_bh, &dirent);
  617. if (status < 0) {
  618. if (status != -ENOENT)
  619. mlog_errno(status);
  620. goto leave;
  621. }
  622. if (OCFS2_I(inode)->ip_blkno != blkno) {
  623. status = -ENOENT;
  624. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  625. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  626. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  627. goto leave;
  628. }
  629. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  630. if (status < 0) {
  631. if (status != -ENOENT)
  632. mlog_errno(status);
  633. goto leave;
  634. }
  635. child_locked = 1;
  636. if (S_ISDIR(inode->i_mode)) {
  637. if (!ocfs2_empty_dir(inode)) {
  638. status = -ENOTEMPTY;
  639. goto leave;
  640. } else if (inode->i_nlink != 2) {
  641. status = -ENOTEMPTY;
  642. goto leave;
  643. }
  644. }
  645. status = ocfs2_remote_dentry_delete(dentry);
  646. if (status < 0) {
  647. /* This remote delete should succeed under all normal
  648. * circumstances. */
  649. mlog_errno(status);
  650. goto leave;
  651. }
  652. if (inode_is_unlinkable(inode)) {
  653. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
  654. orphan_name,
  655. &orphan_entry_bh);
  656. if (status < 0) {
  657. mlog_errno(status);
  658. goto leave;
  659. }
  660. }
  661. handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
  662. if (IS_ERR(handle)) {
  663. status = PTR_ERR(handle);
  664. handle = NULL;
  665. mlog_errno(status);
  666. goto leave;
  667. }
  668. status = ocfs2_journal_access(handle, inode, fe_bh,
  669. OCFS2_JOURNAL_ACCESS_WRITE);
  670. if (status < 0) {
  671. mlog_errno(status);
  672. goto leave;
  673. }
  674. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  675. if (inode_is_unlinkable(inode)) {
  676. status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
  677. orphan_entry_bh, orphan_dir);
  678. if (status < 0) {
  679. mlog_errno(status);
  680. goto leave;
  681. }
  682. }
  683. /* delete the name from the parent dir */
  684. status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
  685. if (status < 0) {
  686. mlog_errno(status);
  687. goto leave;
  688. }
  689. if (S_ISDIR(inode->i_mode))
  690. drop_nlink(inode);
  691. drop_nlink(inode);
  692. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  693. status = ocfs2_journal_dirty(handle, fe_bh);
  694. if (status < 0) {
  695. mlog_errno(status);
  696. goto leave;
  697. }
  698. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  699. if (S_ISDIR(inode->i_mode))
  700. drop_nlink(dir);
  701. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  702. if (status < 0) {
  703. mlog_errno(status);
  704. if (S_ISDIR(inode->i_mode))
  705. inc_nlink(dir);
  706. }
  707. leave:
  708. if (handle)
  709. ocfs2_commit_trans(osb, handle);
  710. if (child_locked)
  711. ocfs2_inode_unlock(inode, 1);
  712. ocfs2_inode_unlock(dir, 1);
  713. if (orphan_dir) {
  714. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  715. ocfs2_inode_unlock(orphan_dir, 1);
  716. mutex_unlock(&orphan_dir->i_mutex);
  717. iput(orphan_dir);
  718. }
  719. brelse(fe_bh);
  720. brelse(dirent_bh);
  721. brelse(parent_node_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_inode_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_inode_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_inode_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_inode_unlock(inode1, 1);
  787. if (inode1 != inode2)
  788. ocfs2_inode_unlock(inode2, 1);
  789. }
  790. static int ocfs2_rename(struct inode *old_dir,
  791. struct dentry *old_dentry,
  792. struct inode *new_dir,
  793. struct dentry *new_dentry)
  794. {
  795. int status = 0, rename_lock = 0, parents_locked = 0;
  796. int old_child_locked = 0, new_child_locked = 0;
  797. struct inode *old_inode = old_dentry->d_inode;
  798. struct inode *new_inode = new_dentry->d_inode;
  799. struct inode *orphan_dir = NULL;
  800. struct ocfs2_dinode *newfe = NULL;
  801. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  802. struct buffer_head *orphan_entry_bh = NULL;
  803. struct buffer_head *newfe_bh = NULL;
  804. struct buffer_head *old_inode_bh = NULL;
  805. struct buffer_head *insert_entry_bh = NULL;
  806. struct ocfs2_super *osb = NULL;
  807. u64 newfe_blkno, old_de_ino;
  808. handle_t *handle = NULL;
  809. struct buffer_head *old_dir_bh = NULL;
  810. struct buffer_head *new_dir_bh = NULL;
  811. struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
  812. *new_de = NULL;
  813. struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
  814. struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
  815. // this is the 1st dirent bh
  816. nlink_t old_dir_nlink = old_dir->i_nlink;
  817. struct ocfs2_dinode *old_di;
  818. /* At some point it might be nice to break this function up a
  819. * bit. */
  820. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  821. old_dir, old_dentry, new_dir, new_dentry,
  822. old_dentry->d_name.len, old_dentry->d_name.name,
  823. new_dentry->d_name.len, new_dentry->d_name.name);
  824. osb = OCFS2_SB(old_dir->i_sb);
  825. if (new_inode) {
  826. if (!igrab(new_inode))
  827. BUG();
  828. }
  829. /* Assume a directory hierarchy thusly:
  830. * a/b/c
  831. * a/d
  832. * a,b,c, and d are all directories.
  833. *
  834. * from cwd of 'a' on both nodes:
  835. * node1: mv b/c d
  836. * node2: mv d b/c
  837. *
  838. * And that's why, just like the VFS, we need a file system
  839. * rename lock. */
  840. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  841. status = ocfs2_rename_lock(osb);
  842. if (status < 0) {
  843. mlog_errno(status);
  844. goto bail;
  845. }
  846. rename_lock = 1;
  847. }
  848. /* if old and new are the same, this'll just do one lock. */
  849. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  850. &new_dir_bh, new_dir);
  851. if (status < 0) {
  852. mlog_errno(status);
  853. goto bail;
  854. }
  855. parents_locked = 1;
  856. /* make sure both dirs have bhs
  857. * get an extra ref on old_dir_bh if old==new */
  858. if (!new_dir_bh) {
  859. if (old_dir_bh) {
  860. new_dir_bh = old_dir_bh;
  861. get_bh(new_dir_bh);
  862. } else {
  863. mlog(ML_ERROR, "no old_dir_bh!\n");
  864. status = -EIO;
  865. goto bail;
  866. }
  867. }
  868. /*
  869. * Aside from allowing a meta data update, the locking here
  870. * also ensures that the downconvert thread on other nodes
  871. * won't have to concurrently downconvert the inode and the
  872. * dentry locks.
  873. */
  874. status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
  875. if (status < 0) {
  876. if (status != -ENOENT)
  877. mlog_errno(status);
  878. goto bail;
  879. }
  880. old_child_locked = 1;
  881. status = ocfs2_remote_dentry_delete(old_dentry);
  882. if (status < 0) {
  883. mlog_errno(status);
  884. goto bail;
  885. }
  886. if (S_ISDIR(old_inode->i_mode)) {
  887. u64 old_inode_parent;
  888. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  889. old_inode, &old_inode_de_bh,
  890. &old_inode_dot_dot_de);
  891. if (status) {
  892. status = -EIO;
  893. goto bail;
  894. }
  895. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  896. status = -EIO;
  897. goto bail;
  898. }
  899. if (!new_inode && new_dir != old_dir &&
  900. new_dir->i_nlink >= OCFS2_LINK_MAX) {
  901. status = -EMLINK;
  902. goto bail;
  903. }
  904. }
  905. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  906. old_dentry->d_name.len,
  907. &old_de_ino);
  908. if (status) {
  909. status = -ENOENT;
  910. goto bail;
  911. }
  912. /*
  913. * Check for inode number is _not_ due to possible IO errors.
  914. * We might rmdir the source, keep it as pwd of some process
  915. * and merrily kill the link to whatever was created under the
  916. * same name. Goodbye sticky bit ;-<
  917. */
  918. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  919. status = -ENOENT;
  920. goto bail;
  921. }
  922. /* check if the target already exists (in which case we need
  923. * to delete it */
  924. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  925. new_dentry->d_name.len,
  926. &newfe_blkno, new_dir, &new_de_bh,
  927. &new_de);
  928. /* The only error we allow here is -ENOENT because the new
  929. * file not existing is perfectly valid. */
  930. if ((status < 0) && (status != -ENOENT)) {
  931. /* If we cannot find the file specified we should just */
  932. /* return the error... */
  933. mlog_errno(status);
  934. goto bail;
  935. }
  936. if (!new_de && new_inode) {
  937. /*
  938. * Target was unlinked by another node while we were
  939. * waiting to get to ocfs2_rename(). There isn't
  940. * anything we can do here to help the situation, so
  941. * bubble up the appropriate error.
  942. */
  943. status = -ENOENT;
  944. goto bail;
  945. }
  946. /* In case we need to overwrite an existing file, we blow it
  947. * away first */
  948. if (new_de) {
  949. /* VFS didn't think there existed an inode here, but
  950. * someone else in the cluster must have raced our
  951. * rename to create one. Today we error cleanly, in
  952. * the future we should consider calling iget to build
  953. * a new struct inode for this entry. */
  954. if (!new_inode) {
  955. status = -EACCES;
  956. mlog(0, "We found an inode for name %.*s but VFS "
  957. "didn't give us one.\n", new_dentry->d_name.len,
  958. new_dentry->d_name.name);
  959. goto bail;
  960. }
  961. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  962. status = -EACCES;
  963. mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
  964. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  965. (unsigned long long)newfe_blkno,
  966. OCFS2_I(new_inode)->ip_flags);
  967. goto bail;
  968. }
  969. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  970. if (status < 0) {
  971. if (status != -ENOENT)
  972. mlog_errno(status);
  973. goto bail;
  974. }
  975. new_child_locked = 1;
  976. status = ocfs2_remote_dentry_delete(new_dentry);
  977. if (status < 0) {
  978. mlog_errno(status);
  979. goto bail;
  980. }
  981. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  982. mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
  983. "newfebh=%p bhblocknr=%llu\n", new_de,
  984. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  985. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  986. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  987. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  988. new_inode,
  989. orphan_name,
  990. &orphan_entry_bh);
  991. if (status < 0) {
  992. mlog_errno(status);
  993. goto bail;
  994. }
  995. }
  996. } else {
  997. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  998. status = ocfs2_check_dir_for_entry(new_dir,
  999. new_dentry->d_name.name,
  1000. new_dentry->d_name.len);
  1001. if (status)
  1002. goto bail;
  1003. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1004. new_dentry->d_name.name,
  1005. new_dentry->d_name.len,
  1006. &insert_entry_bh);
  1007. if (status < 0) {
  1008. mlog_errno(status);
  1009. goto bail;
  1010. }
  1011. }
  1012. handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
  1013. if (IS_ERR(handle)) {
  1014. status = PTR_ERR(handle);
  1015. handle = NULL;
  1016. mlog_errno(status);
  1017. goto bail;
  1018. }
  1019. if (new_de) {
  1020. if (S_ISDIR(new_inode->i_mode)) {
  1021. if (!ocfs2_empty_dir(new_inode) ||
  1022. new_inode->i_nlink != 2) {
  1023. status = -ENOTEMPTY;
  1024. goto bail;
  1025. }
  1026. }
  1027. status = ocfs2_journal_access(handle, new_inode, newfe_bh,
  1028. OCFS2_JOURNAL_ACCESS_WRITE);
  1029. if (status < 0) {
  1030. mlog_errno(status);
  1031. goto bail;
  1032. }
  1033. if (S_ISDIR(new_inode->i_mode) ||
  1034. (newfe->i_links_count == cpu_to_le16(1))){
  1035. status = ocfs2_orphan_add(osb, handle, new_inode,
  1036. newfe, orphan_name,
  1037. orphan_entry_bh, orphan_dir);
  1038. if (status < 0) {
  1039. mlog_errno(status);
  1040. goto bail;
  1041. }
  1042. }
  1043. /* change the dirent to point to the correct inode */
  1044. status = ocfs2_update_entry(new_dir, handle, new_de_bh,
  1045. new_de, old_inode);
  1046. if (status < 0) {
  1047. mlog_errno(status);
  1048. goto bail;
  1049. }
  1050. new_dir->i_version++;
  1051. if (S_ISDIR(new_inode->i_mode))
  1052. newfe->i_links_count = 0;
  1053. else
  1054. le16_add_cpu(&newfe->i_links_count, -1);
  1055. status = ocfs2_journal_dirty(handle, newfe_bh);
  1056. if (status < 0) {
  1057. mlog_errno(status);
  1058. goto bail;
  1059. }
  1060. } else {
  1061. /* if the name was not found in new_dir, add it now */
  1062. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1063. OCFS2_I(old_inode)->ip_blkno,
  1064. new_dir_bh, insert_entry_bh);
  1065. }
  1066. old_inode->i_ctime = CURRENT_TIME;
  1067. mark_inode_dirty(old_inode);
  1068. status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
  1069. OCFS2_JOURNAL_ACCESS_WRITE);
  1070. if (status >= 0) {
  1071. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1072. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1073. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1074. status = ocfs2_journal_dirty(handle, old_inode_bh);
  1075. if (status < 0)
  1076. mlog_errno(status);
  1077. } else
  1078. mlog_errno(status);
  1079. /*
  1080. * Now that the name has been added to new_dir, remove the old name.
  1081. *
  1082. * We don't keep any directory entry context around until now
  1083. * because the insert might have changed the type of directory
  1084. * we're dealing with.
  1085. */
  1086. old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
  1087. old_dentry->d_name.len,
  1088. old_dir, &old_de);
  1089. if (!old_de_bh) {
  1090. status = -EIO;
  1091. goto bail;
  1092. }
  1093. status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
  1094. if (status < 0) {
  1095. mlog_errno(status);
  1096. goto bail;
  1097. }
  1098. if (new_inode) {
  1099. new_inode->i_nlink--;
  1100. new_inode->i_ctime = CURRENT_TIME;
  1101. }
  1102. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1103. if (old_inode_de_bh) {
  1104. status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
  1105. old_inode_dot_dot_de, new_dir);
  1106. old_dir->i_nlink--;
  1107. if (new_inode) {
  1108. new_inode->i_nlink--;
  1109. } else {
  1110. inc_nlink(new_dir);
  1111. mark_inode_dirty(new_dir);
  1112. }
  1113. }
  1114. mark_inode_dirty(old_dir);
  1115. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1116. if (new_inode) {
  1117. mark_inode_dirty(new_inode);
  1118. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1119. }
  1120. if (old_dir != new_dir) {
  1121. /* Keep the same times on both directories.*/
  1122. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1123. /*
  1124. * This will also pick up the i_nlink change from the
  1125. * block above.
  1126. */
  1127. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1128. }
  1129. if (old_dir_nlink != old_dir->i_nlink) {
  1130. if (!old_dir_bh) {
  1131. mlog(ML_ERROR, "need to change nlink for old dir "
  1132. "%llu from %d to %d but bh is NULL!\n",
  1133. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1134. (int)old_dir_nlink, old_dir->i_nlink);
  1135. } else {
  1136. struct ocfs2_dinode *fe;
  1137. status = ocfs2_journal_access(handle, old_dir,
  1138. old_dir_bh,
  1139. OCFS2_JOURNAL_ACCESS_WRITE);
  1140. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1141. fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
  1142. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1143. }
  1144. }
  1145. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1146. status = 0;
  1147. bail:
  1148. if (rename_lock)
  1149. ocfs2_rename_unlock(osb);
  1150. if (handle)
  1151. ocfs2_commit_trans(osb, handle);
  1152. if (parents_locked)
  1153. ocfs2_double_unlock(old_dir, new_dir);
  1154. if (old_child_locked)
  1155. ocfs2_inode_unlock(old_inode, 1);
  1156. if (new_child_locked)
  1157. ocfs2_inode_unlock(new_inode, 1);
  1158. if (orphan_dir) {
  1159. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1160. ocfs2_inode_unlock(orphan_dir, 1);
  1161. mutex_unlock(&orphan_dir->i_mutex);
  1162. iput(orphan_dir);
  1163. }
  1164. if (new_inode)
  1165. sync_mapping_buffers(old_inode->i_mapping);
  1166. if (new_inode)
  1167. iput(new_inode);
  1168. brelse(newfe_bh);
  1169. brelse(old_inode_bh);
  1170. brelse(old_dir_bh);
  1171. brelse(new_dir_bh);
  1172. brelse(new_de_bh);
  1173. brelse(old_de_bh);
  1174. brelse(old_inode_de_bh);
  1175. brelse(orphan_entry_bh);
  1176. brelse(insert_entry_bh);
  1177. mlog_exit(status);
  1178. return status;
  1179. }
  1180. /*
  1181. * we expect i_size = strlen(symname). Copy symname into the file
  1182. * data, including the null terminator.
  1183. */
  1184. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1185. handle_t *handle,
  1186. struct inode *inode,
  1187. const char *symname)
  1188. {
  1189. struct buffer_head **bhs = NULL;
  1190. const char *c;
  1191. struct super_block *sb = osb->sb;
  1192. u64 p_blkno, p_blocks;
  1193. int virtual, blocks, status, i, bytes_left;
  1194. bytes_left = i_size_read(inode) + 1;
  1195. /* we can't trust i_blocks because we're actually going to
  1196. * write i_size + 1 bytes. */
  1197. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1198. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1199. (unsigned long long)inode->i_blocks,
  1200. i_size_read(inode), blocks);
  1201. /* Sanity check -- make sure we're going to fit. */
  1202. if (bytes_left >
  1203. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1204. status = -EIO;
  1205. mlog_errno(status);
  1206. goto bail;
  1207. }
  1208. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1209. if (!bhs) {
  1210. status = -ENOMEM;
  1211. mlog_errno(status);
  1212. goto bail;
  1213. }
  1214. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1215. NULL);
  1216. if (status < 0) {
  1217. mlog_errno(status);
  1218. goto bail;
  1219. }
  1220. /* links can never be larger than one cluster so we know this
  1221. * is all going to be contiguous, but do a sanity check
  1222. * anyway. */
  1223. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1224. status = -EIO;
  1225. mlog_errno(status);
  1226. goto bail;
  1227. }
  1228. virtual = 0;
  1229. while(bytes_left > 0) {
  1230. c = &symname[virtual * sb->s_blocksize];
  1231. bhs[virtual] = sb_getblk(sb, p_blkno);
  1232. if (!bhs[virtual]) {
  1233. status = -ENOMEM;
  1234. mlog_errno(status);
  1235. goto bail;
  1236. }
  1237. ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
  1238. status = ocfs2_journal_access(handle, inode, bhs[virtual],
  1239. OCFS2_JOURNAL_ACCESS_CREATE);
  1240. if (status < 0) {
  1241. mlog_errno(status);
  1242. goto bail;
  1243. }
  1244. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1245. memcpy(bhs[virtual]->b_data, c,
  1246. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1247. bytes_left);
  1248. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1249. if (status < 0) {
  1250. mlog_errno(status);
  1251. goto bail;
  1252. }
  1253. virtual++;
  1254. p_blkno++;
  1255. bytes_left -= sb->s_blocksize;
  1256. }
  1257. status = 0;
  1258. bail:
  1259. if (bhs) {
  1260. for(i = 0; i < blocks; i++)
  1261. brelse(bhs[i]);
  1262. kfree(bhs);
  1263. }
  1264. mlog_exit(status);
  1265. return status;
  1266. }
  1267. static int ocfs2_symlink(struct inode *dir,
  1268. struct dentry *dentry,
  1269. const char *symname)
  1270. {
  1271. int status, l, credits;
  1272. u64 newsize;
  1273. struct ocfs2_super *osb = NULL;
  1274. struct inode *inode = NULL;
  1275. struct super_block *sb;
  1276. struct buffer_head *new_fe_bh = NULL;
  1277. struct buffer_head *de_bh = NULL;
  1278. struct buffer_head *parent_fe_bh = NULL;
  1279. struct ocfs2_dinode *fe = NULL;
  1280. struct ocfs2_dinode *dirfe;
  1281. handle_t *handle = NULL;
  1282. struct ocfs2_alloc_context *inode_ac = NULL;
  1283. struct ocfs2_alloc_context *data_ac = NULL;
  1284. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1285. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1286. sb = dir->i_sb;
  1287. osb = OCFS2_SB(sb);
  1288. l = strlen(symname) + 1;
  1289. credits = ocfs2_calc_symlink_credits(sb);
  1290. /* lock the parent directory */
  1291. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1292. if (status < 0) {
  1293. if (status != -ENOENT)
  1294. mlog_errno(status);
  1295. return status;
  1296. }
  1297. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1298. if (!dirfe->i_links_count) {
  1299. /* can't make a file in a deleted directory. */
  1300. status = -ENOENT;
  1301. goto bail;
  1302. }
  1303. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1304. dentry->d_name.len);
  1305. if (status)
  1306. goto bail;
  1307. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1308. dentry->d_name.name,
  1309. dentry->d_name.len, &de_bh);
  1310. if (status < 0) {
  1311. mlog_errno(status);
  1312. goto bail;
  1313. }
  1314. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1315. if (status < 0) {
  1316. if (status != -ENOSPC)
  1317. mlog_errno(status);
  1318. goto bail;
  1319. }
  1320. /* don't reserve bitmap space for fast symlinks. */
  1321. if (l > ocfs2_fast_symlink_chars(sb)) {
  1322. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  1323. if (status < 0) {
  1324. if (status != -ENOSPC)
  1325. mlog_errno(status);
  1326. goto bail;
  1327. }
  1328. }
  1329. handle = ocfs2_start_trans(osb, credits);
  1330. if (IS_ERR(handle)) {
  1331. status = PTR_ERR(handle);
  1332. handle = NULL;
  1333. mlog_errno(status);
  1334. goto bail;
  1335. }
  1336. status = ocfs2_mknod_locked(osb, dir, dentry,
  1337. S_IFLNK | S_IRWXUGO, 0,
  1338. &new_fe_bh, parent_fe_bh, handle,
  1339. &inode, inode_ac);
  1340. if (status < 0) {
  1341. mlog_errno(status);
  1342. goto bail;
  1343. }
  1344. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1345. inode->i_rdev = 0;
  1346. newsize = l - 1;
  1347. if (l > ocfs2_fast_symlink_chars(sb)) {
  1348. u32 offset = 0;
  1349. inode->i_op = &ocfs2_symlink_inode_operations;
  1350. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1351. new_fe_bh,
  1352. handle, data_ac, NULL,
  1353. NULL);
  1354. if (status < 0) {
  1355. if (status != -ENOSPC && status != -EINTR) {
  1356. mlog(ML_ERROR,
  1357. "Failed to extend file to %llu\n",
  1358. (unsigned long long)newsize);
  1359. mlog_errno(status);
  1360. status = -ENOSPC;
  1361. }
  1362. goto bail;
  1363. }
  1364. i_size_write(inode, newsize);
  1365. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1366. } else {
  1367. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1368. memcpy((char *) fe->id2.i_symlink, symname, l);
  1369. i_size_write(inode, newsize);
  1370. inode->i_blocks = 0;
  1371. }
  1372. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1373. if (status < 0) {
  1374. mlog_errno(status);
  1375. goto bail;
  1376. }
  1377. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1378. status = ocfs2_create_symlink_data(osb, handle, inode,
  1379. symname);
  1380. if (status < 0) {
  1381. mlog_errno(status);
  1382. goto bail;
  1383. }
  1384. }
  1385. status = ocfs2_add_entry(handle, dentry, inode,
  1386. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1387. de_bh);
  1388. if (status < 0) {
  1389. mlog_errno(status);
  1390. goto bail;
  1391. }
  1392. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1393. if (status) {
  1394. mlog_errno(status);
  1395. goto bail;
  1396. }
  1397. insert_inode_hash(inode);
  1398. dentry->d_op = &ocfs2_dentry_ops;
  1399. d_instantiate(dentry, inode);
  1400. bail:
  1401. if (handle)
  1402. ocfs2_commit_trans(osb, handle);
  1403. ocfs2_inode_unlock(dir, 1);
  1404. brelse(new_fe_bh);
  1405. brelse(parent_fe_bh);
  1406. brelse(de_bh);
  1407. if (inode_ac)
  1408. ocfs2_free_alloc_context(inode_ac);
  1409. if (data_ac)
  1410. ocfs2_free_alloc_context(data_ac);
  1411. if ((status < 0) && inode)
  1412. iput(inode);
  1413. mlog_exit(status);
  1414. return status;
  1415. }
  1416. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1417. {
  1418. int status, namelen;
  1419. mlog_entry_void();
  1420. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1421. (long long)blkno);
  1422. if (namelen <= 0) {
  1423. if (namelen)
  1424. status = namelen;
  1425. else
  1426. status = -EINVAL;
  1427. mlog_errno(status);
  1428. goto bail;
  1429. }
  1430. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1431. status = -EINVAL;
  1432. mlog_errno(status);
  1433. goto bail;
  1434. }
  1435. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1436. namelen);
  1437. status = 0;
  1438. bail:
  1439. mlog_exit(status);
  1440. return status;
  1441. }
  1442. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1443. struct inode **ret_orphan_dir,
  1444. struct inode *inode,
  1445. char *name,
  1446. struct buffer_head **de_bh)
  1447. {
  1448. struct inode *orphan_dir_inode;
  1449. struct buffer_head *orphan_dir_bh = NULL;
  1450. int status = 0;
  1451. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1452. if (status < 0) {
  1453. mlog_errno(status);
  1454. return status;
  1455. }
  1456. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1457. ORPHAN_DIR_SYSTEM_INODE,
  1458. osb->slot_num);
  1459. if (!orphan_dir_inode) {
  1460. status = -ENOENT;
  1461. mlog_errno(status);
  1462. return status;
  1463. }
  1464. mutex_lock(&orphan_dir_inode->i_mutex);
  1465. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1466. if (status < 0) {
  1467. mlog_errno(status);
  1468. goto leave;
  1469. }
  1470. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1471. orphan_dir_bh, name,
  1472. OCFS2_ORPHAN_NAMELEN, de_bh);
  1473. if (status < 0) {
  1474. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1475. mlog_errno(status);
  1476. goto leave;
  1477. }
  1478. *ret_orphan_dir = orphan_dir_inode;
  1479. leave:
  1480. if (status) {
  1481. mutex_unlock(&orphan_dir_inode->i_mutex);
  1482. iput(orphan_dir_inode);
  1483. }
  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(orphan_dir_inode,
  1501. OCFS2_I(orphan_dir_inode)->ip_blkno,
  1502. &orphan_dir_bh);
  1503. if (status < 0) {
  1504. mlog_errno(status);
  1505. goto leave;
  1506. }
  1507. status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
  1508. OCFS2_JOURNAL_ACCESS_WRITE);
  1509. if (status < 0) {
  1510. mlog_errno(status);
  1511. goto leave;
  1512. }
  1513. /* we're a cluster, and nlink can change on disk from
  1514. * underneath us... */
  1515. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1516. if (S_ISDIR(inode->i_mode))
  1517. le16_add_cpu(&orphan_fe->i_links_count, 1);
  1518. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1519. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1520. if (status < 0) {
  1521. mlog_errno(status);
  1522. goto leave;
  1523. }
  1524. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1525. OCFS2_ORPHAN_NAMELEN, inode,
  1526. OCFS2_I(inode)->ip_blkno,
  1527. orphan_dir_bh, de_bh);
  1528. if (status < 0) {
  1529. mlog_errno(status);
  1530. goto leave;
  1531. }
  1532. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1533. /* Record which orphan dir our inode now resides
  1534. * in. delete_inode will use this to determine which orphan
  1535. * dir to lock. */
  1536. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1537. mlog(0, "Inode %llu orphaned in slot %d\n",
  1538. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1539. leave:
  1540. brelse(orphan_dir_bh);
  1541. mlog_exit(status);
  1542. return status;
  1543. }
  1544. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1545. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1546. handle_t *handle,
  1547. struct inode *orphan_dir_inode,
  1548. struct inode *inode,
  1549. struct buffer_head *orphan_dir_bh)
  1550. {
  1551. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1552. struct ocfs2_dinode *orphan_fe;
  1553. int status = 0;
  1554. struct buffer_head *target_de_bh = NULL;
  1555. struct ocfs2_dir_entry *target_de = NULL;
  1556. mlog_entry_void();
  1557. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1558. if (status < 0) {
  1559. mlog_errno(status);
  1560. goto leave;
  1561. }
  1562. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1563. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1564. OCFS2_ORPHAN_NAMELEN);
  1565. /* find it's spot in the orphan directory */
  1566. target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
  1567. orphan_dir_inode, &target_de);
  1568. if (!target_de_bh) {
  1569. status = -ENOENT;
  1570. mlog_errno(status);
  1571. goto leave;
  1572. }
  1573. /* remove it from the orphan directory */
  1574. status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
  1575. target_de_bh);
  1576. if (status < 0) {
  1577. mlog_errno(status);
  1578. goto leave;
  1579. }
  1580. status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
  1581. OCFS2_JOURNAL_ACCESS_WRITE);
  1582. if (status < 0) {
  1583. mlog_errno(status);
  1584. goto leave;
  1585. }
  1586. /* do the i_nlink dance! :) */
  1587. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1588. if (S_ISDIR(inode->i_mode))
  1589. le16_add_cpu(&orphan_fe->i_links_count, -1);
  1590. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1591. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1592. if (status < 0) {
  1593. mlog_errno(status);
  1594. goto leave;
  1595. }
  1596. leave:
  1597. brelse(target_de_bh);
  1598. mlog_exit(status);
  1599. return status;
  1600. }
  1601. const struct inode_operations ocfs2_dir_iops = {
  1602. .create = ocfs2_create,
  1603. .lookup = ocfs2_lookup,
  1604. .link = ocfs2_link,
  1605. .unlink = ocfs2_unlink,
  1606. .rmdir = ocfs2_unlink,
  1607. .symlink = ocfs2_symlink,
  1608. .mkdir = ocfs2_mkdir,
  1609. .mknod = ocfs2_mknod,
  1610. .rename = ocfs2_rename,
  1611. .setattr = ocfs2_setattr,
  1612. .getattr = ocfs2_getattr,
  1613. .permission = ocfs2_permission,
  1614. .setxattr = generic_setxattr,
  1615. .getxattr = generic_getxattr,
  1616. .listxattr = ocfs2_listxattr,
  1617. .removexattr = generic_removexattr,
  1618. };