namei.c 45 KB

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