namei.c 48 KB

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