namei.c 47 KB

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