namei.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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. #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 "ocfs2_trace.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. 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. u64 blkno,
  74. char *name,
  75. struct ocfs2_dir_lookup_result *lookup);
  76. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  77. handle_t *handle,
  78. struct inode *inode,
  79. struct buffer_head *fe_bh,
  80. char *name,
  81. struct ocfs2_dir_lookup_result *lookup,
  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. unsigned int flags)
  91. {
  92. int status;
  93. u64 blkno;
  94. struct inode *inode = NULL;
  95. struct dentry *ret;
  96. struct ocfs2_inode_info *oi;
  97. trace_ocfs2_lookup(dir, dentry, dentry->d_name.len,
  98. dentry->d_name.name,
  99. (unsigned long long)OCFS2_I(dir)->ip_blkno, 0);
  100. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  101. ret = ERR_PTR(-ENAMETOOLONG);
  102. goto bail;
  103. }
  104. status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
  105. if (status < 0) {
  106. if (status != -ENOENT)
  107. mlog_errno(status);
  108. ret = ERR_PTR(status);
  109. goto bail;
  110. }
  111. status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
  112. dentry->d_name.len, &blkno);
  113. if (status < 0)
  114. goto bail_add;
  115. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
  116. if (IS_ERR(inode)) {
  117. ret = ERR_PTR(-EACCES);
  118. goto bail_unlock;
  119. }
  120. oi = OCFS2_I(inode);
  121. /* Clear any orphaned state... If we were able to look up the
  122. * inode from a directory, it certainly can't be orphaned. We
  123. * might have the bad state from a node which intended to
  124. * orphan this inode but crashed before it could commit the
  125. * unlink. */
  126. spin_lock(&oi->ip_lock);
  127. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  128. spin_unlock(&oi->ip_lock);
  129. bail_add:
  130. ret = d_splice_alias(inode, dentry);
  131. if (inode) {
  132. /*
  133. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  134. * dentry, it will d_move() it on top of ourse. The
  135. * return value will indicate this however, so in
  136. * those cases, we switch them around for the locking
  137. * code.
  138. *
  139. * NOTE: This dentry already has ->d_op set from
  140. * ocfs2_get_parent() and ocfs2_get_dentry()
  141. */
  142. if (ret)
  143. dentry = ret;
  144. status = ocfs2_dentry_attach_lock(dentry, inode,
  145. OCFS2_I(dir)->ip_blkno);
  146. if (status) {
  147. mlog_errno(status);
  148. ret = ERR_PTR(status);
  149. goto bail_unlock;
  150. }
  151. } else
  152. ocfs2_dentry_attach_gen(dentry);
  153. bail_unlock:
  154. /* Don't drop the cluster lock until *after* the d_add --
  155. * unlink on another node will message us to remove that
  156. * dentry under this lock so otherwise we can race this with
  157. * the downconvert thread and have a stale dentry. */
  158. ocfs2_inode_unlock(dir, 0);
  159. bail:
  160. trace_ocfs2_lookup_ret(ret);
  161. return ret;
  162. }
  163. static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
  164. {
  165. struct inode *inode;
  166. inode = new_inode(dir->i_sb);
  167. if (!inode) {
  168. mlog(ML_ERROR, "new_inode failed!\n");
  169. return NULL;
  170. }
  171. /* populate as many fields early on as possible - many of
  172. * these are used by the support functions here and in
  173. * callers. */
  174. if (S_ISDIR(mode))
  175. set_nlink(inode, 2);
  176. inode_init_owner(inode, dir, mode);
  177. dquot_initialize(inode);
  178. return inode;
  179. }
  180. static int ocfs2_mknod(struct inode *dir,
  181. struct dentry *dentry,
  182. umode_t mode,
  183. dev_t dev)
  184. {
  185. int status = 0;
  186. struct buffer_head *parent_fe_bh = NULL;
  187. handle_t *handle = NULL;
  188. struct ocfs2_super *osb;
  189. struct ocfs2_dinode *dirfe;
  190. struct buffer_head *new_fe_bh = NULL;
  191. struct inode *inode = NULL;
  192. struct ocfs2_alloc_context *inode_ac = NULL;
  193. struct ocfs2_alloc_context *data_ac = NULL;
  194. struct ocfs2_alloc_context *meta_ac = NULL;
  195. int want_clusters = 0;
  196. int want_meta = 0;
  197. int xattr_credits = 0;
  198. struct ocfs2_security_xattr_info si = {
  199. .enable = 1,
  200. };
  201. int did_quota_inode = 0;
  202. struct ocfs2_dir_lookup_result lookup = { NULL, };
  203. sigset_t oldset;
  204. int did_block_signals = 0;
  205. struct posix_acl *default_acl = NULL, *acl = NULL;
  206. trace_ocfs2_mknod(dir, dentry, dentry->d_name.len, dentry->d_name.name,
  207. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  208. (unsigned long)dev, mode);
  209. dquot_initialize(dir);
  210. /* get our super block */
  211. osb = OCFS2_SB(dir->i_sb);
  212. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  213. if (status < 0) {
  214. if (status != -ENOENT)
  215. mlog_errno(status);
  216. return status;
  217. }
  218. if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
  219. status = -EMLINK;
  220. goto leave;
  221. }
  222. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  223. if (!ocfs2_read_links_count(dirfe)) {
  224. /* can't make a file in a deleted directory. */
  225. status = -ENOENT;
  226. goto leave;
  227. }
  228. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  229. dentry->d_name.len);
  230. if (status)
  231. goto leave;
  232. /* get a spot inside the dir. */
  233. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  234. dentry->d_name.name,
  235. dentry->d_name.len, &lookup);
  236. if (status < 0) {
  237. mlog_errno(status);
  238. goto leave;
  239. }
  240. /* reserve an inode spot */
  241. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  242. if (status < 0) {
  243. if (status != -ENOSPC)
  244. mlog_errno(status);
  245. goto leave;
  246. }
  247. inode = ocfs2_get_init_inode(dir, mode);
  248. if (!inode) {
  249. status = -ENOMEM;
  250. mlog_errno(status);
  251. goto leave;
  252. }
  253. /* get security xattr */
  254. status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
  255. if (status) {
  256. if (status == -EOPNOTSUPP)
  257. si.enable = 0;
  258. else {
  259. mlog_errno(status);
  260. goto leave;
  261. }
  262. }
  263. /* calculate meta data/clusters for setting security and acl xattr */
  264. status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
  265. &si, &want_clusters,
  266. &xattr_credits, &want_meta);
  267. if (status < 0) {
  268. mlog_errno(status);
  269. goto leave;
  270. }
  271. /* Reserve a cluster if creating an extent based directory. */
  272. if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
  273. want_clusters += 1;
  274. /* Dir indexing requires extra space as well */
  275. if (ocfs2_supports_indexed_dirs(osb))
  276. want_meta++;
  277. }
  278. status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
  279. if (status < 0) {
  280. if (status != -ENOSPC)
  281. mlog_errno(status);
  282. goto leave;
  283. }
  284. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  285. if (status < 0) {
  286. if (status != -ENOSPC)
  287. mlog_errno(status);
  288. goto leave;
  289. }
  290. status = posix_acl_create(dir, &mode, &default_acl, &acl);
  291. if (status) {
  292. mlog_errno(status);
  293. goto leave;
  294. }
  295. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
  296. S_ISDIR(mode),
  297. xattr_credits));
  298. if (IS_ERR(handle)) {
  299. status = PTR_ERR(handle);
  300. handle = NULL;
  301. mlog_errno(status);
  302. goto leave;
  303. }
  304. /* Starting to change things, restart is no longer possible. */
  305. ocfs2_block_signals(&oldset);
  306. did_block_signals = 1;
  307. status = dquot_alloc_inode(inode);
  308. if (status)
  309. goto leave;
  310. did_quota_inode = 1;
  311. /* do the real work now. */
  312. status = ocfs2_mknod_locked(osb, dir, inode, dev,
  313. &new_fe_bh, parent_fe_bh, handle,
  314. inode_ac);
  315. if (status < 0) {
  316. mlog_errno(status);
  317. goto leave;
  318. }
  319. if (S_ISDIR(mode)) {
  320. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  321. new_fe_bh, data_ac, meta_ac);
  322. if (status < 0) {
  323. mlog_errno(status);
  324. goto leave;
  325. }
  326. status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
  327. parent_fe_bh,
  328. OCFS2_JOURNAL_ACCESS_WRITE);
  329. if (status < 0) {
  330. mlog_errno(status);
  331. goto leave;
  332. }
  333. ocfs2_add_links_count(dirfe, 1);
  334. ocfs2_journal_dirty(handle, parent_fe_bh);
  335. inc_nlink(dir);
  336. }
  337. if (default_acl) {
  338. status = ocfs2_set_acl(handle, inode, new_fe_bh,
  339. ACL_TYPE_DEFAULT, default_acl,
  340. meta_ac, data_ac);
  341. }
  342. if (!status && acl) {
  343. status = ocfs2_set_acl(handle, inode, new_fe_bh,
  344. ACL_TYPE_ACCESS, acl,
  345. meta_ac, data_ac);
  346. }
  347. if (status < 0) {
  348. mlog_errno(status);
  349. goto leave;
  350. }
  351. if (si.enable) {
  352. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  353. meta_ac, data_ac);
  354. if (status < 0) {
  355. mlog_errno(status);
  356. goto leave;
  357. }
  358. }
  359. /*
  360. * Do this before adding the entry to the directory. We add
  361. * also set d_op after success so that ->d_iput() will cleanup
  362. * the dentry lock even if ocfs2_add_entry() fails below.
  363. */
  364. status = ocfs2_dentry_attach_lock(dentry, inode,
  365. OCFS2_I(dir)->ip_blkno);
  366. if (status) {
  367. mlog_errno(status);
  368. goto leave;
  369. }
  370. status = ocfs2_add_entry(handle, dentry, inode,
  371. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  372. &lookup);
  373. if (status < 0) {
  374. mlog_errno(status);
  375. goto leave;
  376. }
  377. insert_inode_hash(inode);
  378. d_instantiate(dentry, inode);
  379. status = 0;
  380. leave:
  381. if (default_acl)
  382. posix_acl_release(default_acl);
  383. if (acl)
  384. posix_acl_release(acl);
  385. if (status < 0 && did_quota_inode)
  386. dquot_free_inode(inode);
  387. if (handle)
  388. ocfs2_commit_trans(osb, handle);
  389. ocfs2_inode_unlock(dir, 1);
  390. if (did_block_signals)
  391. ocfs2_unblock_signals(&oldset);
  392. brelse(new_fe_bh);
  393. brelse(parent_fe_bh);
  394. kfree(si.value);
  395. ocfs2_free_dir_lookup_result(&lookup);
  396. if (inode_ac)
  397. ocfs2_free_alloc_context(inode_ac);
  398. if (data_ac)
  399. ocfs2_free_alloc_context(data_ac);
  400. if (meta_ac)
  401. ocfs2_free_alloc_context(meta_ac);
  402. /*
  403. * We should call iput after the i_mutex of the bitmap been
  404. * unlocked in ocfs2_free_alloc_context, or the
  405. * ocfs2_delete_inode will mutex_lock again.
  406. */
  407. if ((status < 0) && inode) {
  408. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
  409. clear_nlink(inode);
  410. iput(inode);
  411. }
  412. if (status)
  413. mlog_errno(status);
  414. return status;
  415. }
  416. static int __ocfs2_mknod_locked(struct inode *dir,
  417. struct inode *inode,
  418. dev_t dev,
  419. struct buffer_head **new_fe_bh,
  420. struct buffer_head *parent_fe_bh,
  421. handle_t *handle,
  422. struct ocfs2_alloc_context *inode_ac,
  423. u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit)
  424. {
  425. int status = 0;
  426. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  427. struct ocfs2_dinode *fe = NULL;
  428. struct ocfs2_extent_list *fel;
  429. u16 feat;
  430. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  431. *new_fe_bh = NULL;
  432. /* populate as many fields early on as possible - many of
  433. * these are used by the support functions here and in
  434. * callers. */
  435. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  436. OCFS2_I(inode)->ip_blkno = fe_blkno;
  437. spin_lock(&osb->osb_lock);
  438. inode->i_generation = osb->s_next_generation++;
  439. spin_unlock(&osb->osb_lock);
  440. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  441. if (!*new_fe_bh) {
  442. status = -ENOMEM;
  443. mlog_errno(status);
  444. goto leave;
  445. }
  446. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
  447. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  448. *new_fe_bh,
  449. OCFS2_JOURNAL_ACCESS_CREATE);
  450. if (status < 0) {
  451. mlog_errno(status);
  452. goto leave;
  453. }
  454. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  455. memset(fe, 0, osb->sb->s_blocksize);
  456. fe->i_generation = cpu_to_le32(inode->i_generation);
  457. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  458. fe->i_blkno = cpu_to_le64(fe_blkno);
  459. fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
  460. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  461. fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
  462. fe->i_uid = cpu_to_le32(i_uid_read(inode));
  463. fe->i_gid = cpu_to_le32(i_gid_read(inode));
  464. fe->i_mode = cpu_to_le16(inode->i_mode);
  465. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  466. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  467. ocfs2_set_links_count(fe, inode->i_nlink);
  468. fe->i_last_eb_blk = 0;
  469. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  470. fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL);
  471. fe->i_atime = fe->i_ctime = fe->i_mtime =
  472. cpu_to_le64(CURRENT_TIME.tv_sec);
  473. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  474. cpu_to_le32(CURRENT_TIME.tv_nsec);
  475. fe->i_dtime = 0;
  476. /*
  477. * If supported, directories start with inline data. If inline
  478. * isn't supported, but indexing is, we start them as indexed.
  479. */
  480. feat = le16_to_cpu(fe->i_dyn_features);
  481. if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
  482. fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
  483. fe->id2.i_data.id_count = cpu_to_le16(
  484. ocfs2_max_inline_data_with_xattr(osb->sb, fe));
  485. } else {
  486. fel = &fe->id2.i_list;
  487. fel->l_tree_depth = 0;
  488. fel->l_next_free_rec = 0;
  489. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  490. }
  491. ocfs2_journal_dirty(handle, *new_fe_bh);
  492. ocfs2_populate_inode(inode, fe, 1);
  493. ocfs2_ci_set_new(osb, INODE_CACHE(inode));
  494. if (!ocfs2_mount_local(osb)) {
  495. status = ocfs2_create_new_inode_locks(inode);
  496. if (status < 0)
  497. mlog_errno(status);
  498. }
  499. oi->i_sync_tid = handle->h_transaction->t_tid;
  500. oi->i_datasync_tid = handle->h_transaction->t_tid;
  501. leave:
  502. if (status < 0) {
  503. if (*new_fe_bh) {
  504. brelse(*new_fe_bh);
  505. *new_fe_bh = NULL;
  506. }
  507. }
  508. if (status)
  509. mlog_errno(status);
  510. return status;
  511. }
  512. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  513. struct inode *dir,
  514. struct inode *inode,
  515. dev_t dev,
  516. struct buffer_head **new_fe_bh,
  517. struct buffer_head *parent_fe_bh,
  518. handle_t *handle,
  519. struct ocfs2_alloc_context *inode_ac)
  520. {
  521. int status = 0;
  522. u64 suballoc_loc, fe_blkno = 0;
  523. u16 suballoc_bit;
  524. *new_fe_bh = NULL;
  525. status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
  526. inode_ac, &suballoc_loc,
  527. &suballoc_bit, &fe_blkno);
  528. if (status < 0) {
  529. mlog_errno(status);
  530. return status;
  531. }
  532. return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
  533. parent_fe_bh, handle, inode_ac,
  534. fe_blkno, suballoc_loc, suballoc_bit);
  535. }
  536. static int ocfs2_mkdir(struct inode *dir,
  537. struct dentry *dentry,
  538. umode_t mode)
  539. {
  540. int ret;
  541. trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name,
  542. OCFS2_I(dir)->ip_blkno, mode);
  543. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  544. if (ret)
  545. mlog_errno(ret);
  546. return ret;
  547. }
  548. static int ocfs2_create(struct inode *dir,
  549. struct dentry *dentry,
  550. umode_t mode,
  551. bool excl)
  552. {
  553. int ret;
  554. trace_ocfs2_create(dir, dentry, dentry->d_name.len, dentry->d_name.name,
  555. (unsigned long long)OCFS2_I(dir)->ip_blkno, mode);
  556. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  557. if (ret)
  558. mlog_errno(ret);
  559. return ret;
  560. }
  561. static int ocfs2_link(struct dentry *old_dentry,
  562. struct inode *dir,
  563. struct dentry *dentry)
  564. {
  565. handle_t *handle;
  566. struct inode *inode = old_dentry->d_inode;
  567. int err;
  568. struct buffer_head *fe_bh = NULL;
  569. struct buffer_head *parent_fe_bh = NULL;
  570. struct ocfs2_dinode *fe = NULL;
  571. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  572. struct ocfs2_dir_lookup_result lookup = { NULL, };
  573. sigset_t oldset;
  574. u64 old_de_ino;
  575. trace_ocfs2_link((unsigned long long)OCFS2_I(inode)->ip_blkno,
  576. old_dentry->d_name.len, old_dentry->d_name.name,
  577. dentry->d_name.len, dentry->d_name.name);
  578. if (S_ISDIR(inode->i_mode))
  579. return -EPERM;
  580. dquot_initialize(dir);
  581. err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
  582. if (err < 0) {
  583. if (err != -ENOENT)
  584. mlog_errno(err);
  585. return err;
  586. }
  587. if (!dir->i_nlink) {
  588. err = -ENOENT;
  589. goto out;
  590. }
  591. err = ocfs2_lookup_ino_from_name(dir, old_dentry->d_name.name,
  592. old_dentry->d_name.len, &old_de_ino);
  593. if (err) {
  594. err = -ENOENT;
  595. goto out;
  596. }
  597. /*
  598. * Check whether another node removed the source inode while we
  599. * were in the vfs.
  600. */
  601. if (old_de_ino != OCFS2_I(inode)->ip_blkno) {
  602. err = -ENOENT;
  603. goto out;
  604. }
  605. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  606. dentry->d_name.len);
  607. if (err)
  608. goto out;
  609. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  610. dentry->d_name.name,
  611. dentry->d_name.len, &lookup);
  612. if (err < 0) {
  613. mlog_errno(err);
  614. goto out;
  615. }
  616. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  617. if (err < 0) {
  618. if (err != -ENOENT)
  619. mlog_errno(err);
  620. goto out;
  621. }
  622. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  623. if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
  624. err = -EMLINK;
  625. goto out_unlock_inode;
  626. }
  627. handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
  628. if (IS_ERR(handle)) {
  629. err = PTR_ERR(handle);
  630. handle = NULL;
  631. mlog_errno(err);
  632. goto out_unlock_inode;
  633. }
  634. /* Starting to change things, restart is no longer possible. */
  635. ocfs2_block_signals(&oldset);
  636. err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  637. OCFS2_JOURNAL_ACCESS_WRITE);
  638. if (err < 0) {
  639. mlog_errno(err);
  640. goto out_commit;
  641. }
  642. inc_nlink(inode);
  643. inode->i_ctime = CURRENT_TIME;
  644. ocfs2_set_links_count(fe, inode->i_nlink);
  645. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  646. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  647. ocfs2_journal_dirty(handle, fe_bh);
  648. err = ocfs2_add_entry(handle, dentry, inode,
  649. OCFS2_I(inode)->ip_blkno,
  650. parent_fe_bh, &lookup);
  651. if (err) {
  652. ocfs2_add_links_count(fe, -1);
  653. drop_nlink(inode);
  654. mlog_errno(err);
  655. goto out_commit;
  656. }
  657. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  658. if (err) {
  659. mlog_errno(err);
  660. goto out_commit;
  661. }
  662. ihold(inode);
  663. d_instantiate(dentry, inode);
  664. out_commit:
  665. ocfs2_commit_trans(osb, handle);
  666. ocfs2_unblock_signals(&oldset);
  667. out_unlock_inode:
  668. ocfs2_inode_unlock(inode, 1);
  669. out:
  670. ocfs2_inode_unlock(dir, 1);
  671. brelse(fe_bh);
  672. brelse(parent_fe_bh);
  673. ocfs2_free_dir_lookup_result(&lookup);
  674. if (err)
  675. mlog_errno(err);
  676. return err;
  677. }
  678. /*
  679. * Takes and drops an exclusive lock on the given dentry. This will
  680. * force other nodes to drop it.
  681. */
  682. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  683. {
  684. int ret;
  685. ret = ocfs2_dentry_lock(dentry, 1);
  686. if (ret)
  687. mlog_errno(ret);
  688. else
  689. ocfs2_dentry_unlock(dentry, 1);
  690. return ret;
  691. }
  692. static inline int ocfs2_inode_is_unlinkable(struct inode *inode)
  693. {
  694. if (S_ISDIR(inode->i_mode)) {
  695. if (inode->i_nlink == 2)
  696. return 1;
  697. return 0;
  698. }
  699. if (inode->i_nlink == 1)
  700. return 1;
  701. return 0;
  702. }
  703. static int ocfs2_unlink(struct inode *dir,
  704. struct dentry *dentry)
  705. {
  706. int status;
  707. int child_locked = 0;
  708. bool is_unlinkable = false;
  709. struct inode *inode = dentry->d_inode;
  710. struct inode *orphan_dir = NULL;
  711. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  712. u64 blkno;
  713. struct ocfs2_dinode *fe = NULL;
  714. struct buffer_head *fe_bh = NULL;
  715. struct buffer_head *parent_node_bh = NULL;
  716. handle_t *handle = NULL;
  717. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  718. struct ocfs2_dir_lookup_result lookup = { NULL, };
  719. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  720. trace_ocfs2_unlink(dir, dentry, dentry->d_name.len,
  721. dentry->d_name.name,
  722. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  723. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  724. dquot_initialize(dir);
  725. BUG_ON(dentry->d_parent->d_inode != dir);
  726. if (inode == osb->root_inode)
  727. return -EPERM;
  728. status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
  729. OI_LS_PARENT);
  730. if (status < 0) {
  731. if (status != -ENOENT)
  732. mlog_errno(status);
  733. return status;
  734. }
  735. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  736. dentry->d_name.len, &blkno, dir,
  737. &lookup);
  738. if (status < 0) {
  739. if (status != -ENOENT)
  740. mlog_errno(status);
  741. goto leave;
  742. }
  743. if (OCFS2_I(inode)->ip_blkno != blkno) {
  744. status = -ENOENT;
  745. trace_ocfs2_unlink_noent(
  746. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  747. (unsigned long long)blkno,
  748. OCFS2_I(inode)->ip_flags);
  749. goto leave;
  750. }
  751. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  752. if (status < 0) {
  753. if (status != -ENOENT)
  754. mlog_errno(status);
  755. goto leave;
  756. }
  757. child_locked = 1;
  758. if (S_ISDIR(inode->i_mode)) {
  759. if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
  760. status = -ENOTEMPTY;
  761. goto leave;
  762. }
  763. }
  764. status = ocfs2_remote_dentry_delete(dentry);
  765. if (status < 0) {
  766. /* This remote delete should succeed under all normal
  767. * circumstances. */
  768. mlog_errno(status);
  769. goto leave;
  770. }
  771. if (ocfs2_inode_is_unlinkable(inode)) {
  772. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  773. OCFS2_I(inode)->ip_blkno,
  774. orphan_name, &orphan_insert);
  775. if (status < 0) {
  776. mlog_errno(status);
  777. goto leave;
  778. }
  779. is_unlinkable = true;
  780. }
  781. handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
  782. if (IS_ERR(handle)) {
  783. status = PTR_ERR(handle);
  784. handle = NULL;
  785. mlog_errno(status);
  786. goto leave;
  787. }
  788. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  789. OCFS2_JOURNAL_ACCESS_WRITE);
  790. if (status < 0) {
  791. mlog_errno(status);
  792. goto leave;
  793. }
  794. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  795. /* delete the name from the parent dir */
  796. status = ocfs2_delete_entry(handle, dir, &lookup);
  797. if (status < 0) {
  798. mlog_errno(status);
  799. goto leave;
  800. }
  801. if (S_ISDIR(inode->i_mode))
  802. drop_nlink(inode);
  803. drop_nlink(inode);
  804. ocfs2_set_links_count(fe, inode->i_nlink);
  805. ocfs2_journal_dirty(handle, fe_bh);
  806. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  807. if (S_ISDIR(inode->i_mode))
  808. drop_nlink(dir);
  809. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  810. if (status < 0) {
  811. mlog_errno(status);
  812. if (S_ISDIR(inode->i_mode))
  813. inc_nlink(dir);
  814. goto leave;
  815. }
  816. if (is_unlinkable) {
  817. status = ocfs2_orphan_add(osb, handle, inode, fe_bh,
  818. orphan_name, &orphan_insert, orphan_dir);
  819. if (status < 0)
  820. mlog_errno(status);
  821. }
  822. leave:
  823. if (handle)
  824. ocfs2_commit_trans(osb, handle);
  825. if (child_locked)
  826. ocfs2_inode_unlock(inode, 1);
  827. ocfs2_inode_unlock(dir, 1);
  828. if (orphan_dir) {
  829. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  830. ocfs2_inode_unlock(orphan_dir, 1);
  831. mutex_unlock(&orphan_dir->i_mutex);
  832. iput(orphan_dir);
  833. }
  834. brelse(fe_bh);
  835. brelse(parent_node_bh);
  836. ocfs2_free_dir_lookup_result(&orphan_insert);
  837. ocfs2_free_dir_lookup_result(&lookup);
  838. if (status && (status != -ENOTEMPTY) && (status != -ENOENT))
  839. mlog_errno(status);
  840. return status;
  841. }
  842. /*
  843. * The only place this should be used is rename!
  844. * if they have the same id, then the 1st one is the only one locked.
  845. */
  846. static int ocfs2_double_lock(struct ocfs2_super *osb,
  847. struct buffer_head **bh1,
  848. struct inode *inode1,
  849. struct buffer_head **bh2,
  850. struct inode *inode2)
  851. {
  852. int status;
  853. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  854. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  855. struct buffer_head **tmpbh;
  856. struct inode *tmpinode;
  857. trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
  858. (unsigned long long)oi2->ip_blkno);
  859. if (*bh1)
  860. *bh1 = NULL;
  861. if (*bh2)
  862. *bh2 = NULL;
  863. /* we always want to lock the one with the lower lockid first. */
  864. if (oi1->ip_blkno != oi2->ip_blkno) {
  865. if (oi1->ip_blkno < oi2->ip_blkno) {
  866. /* switch id1 and id2 around */
  867. tmpbh = bh2;
  868. bh2 = bh1;
  869. bh1 = tmpbh;
  870. tmpinode = inode2;
  871. inode2 = inode1;
  872. inode1 = tmpinode;
  873. }
  874. /* lock id2 */
  875. status = ocfs2_inode_lock_nested(inode2, bh2, 1,
  876. OI_LS_RENAME1);
  877. if (status < 0) {
  878. if (status != -ENOENT)
  879. mlog_errno(status);
  880. goto bail;
  881. }
  882. }
  883. /* lock id1 */
  884. status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
  885. if (status < 0) {
  886. /*
  887. * An error return must mean that no cluster locks
  888. * were held on function exit.
  889. */
  890. if (oi1->ip_blkno != oi2->ip_blkno) {
  891. ocfs2_inode_unlock(inode2, 1);
  892. brelse(*bh2);
  893. *bh2 = NULL;
  894. }
  895. if (status != -ENOENT)
  896. mlog_errno(status);
  897. }
  898. trace_ocfs2_double_lock_end(
  899. (unsigned long long)OCFS2_I(inode1)->ip_blkno,
  900. (unsigned long long)OCFS2_I(inode2)->ip_blkno);
  901. bail:
  902. if (status)
  903. mlog_errno(status);
  904. return status;
  905. }
  906. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  907. {
  908. ocfs2_inode_unlock(inode1, 1);
  909. if (inode1 != inode2)
  910. ocfs2_inode_unlock(inode2, 1);
  911. }
  912. static int ocfs2_rename(struct inode *old_dir,
  913. struct dentry *old_dentry,
  914. struct inode *new_dir,
  915. struct dentry *new_dentry)
  916. {
  917. int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
  918. int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
  919. struct inode *old_inode = old_dentry->d_inode;
  920. struct inode *new_inode = new_dentry->d_inode;
  921. struct inode *orphan_dir = NULL;
  922. struct ocfs2_dinode *newfe = NULL;
  923. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  924. struct buffer_head *newfe_bh = NULL;
  925. struct buffer_head *old_inode_bh = NULL;
  926. struct ocfs2_super *osb = NULL;
  927. u64 newfe_blkno, old_de_ino;
  928. handle_t *handle = NULL;
  929. struct buffer_head *old_dir_bh = NULL;
  930. struct buffer_head *new_dir_bh = NULL;
  931. u32 old_dir_nlink = old_dir->i_nlink;
  932. struct ocfs2_dinode *old_di;
  933. struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
  934. struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
  935. struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
  936. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  937. struct ocfs2_dir_lookup_result target_insert = { NULL, };
  938. /* At some point it might be nice to break this function up a
  939. * bit. */
  940. trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry,
  941. old_dentry->d_name.len, old_dentry->d_name.name,
  942. new_dentry->d_name.len, new_dentry->d_name.name);
  943. dquot_initialize(old_dir);
  944. dquot_initialize(new_dir);
  945. osb = OCFS2_SB(old_dir->i_sb);
  946. if (new_inode) {
  947. if (!igrab(new_inode))
  948. BUG();
  949. }
  950. /* Assume a directory hierarchy thusly:
  951. * a/b/c
  952. * a/d
  953. * a,b,c, and d are all directories.
  954. *
  955. * from cwd of 'a' on both nodes:
  956. * node1: mv b/c d
  957. * node2: mv d b/c
  958. *
  959. * And that's why, just like the VFS, we need a file system
  960. * rename lock. */
  961. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  962. status = ocfs2_rename_lock(osb);
  963. if (status < 0) {
  964. mlog_errno(status);
  965. goto bail;
  966. }
  967. rename_lock = 1;
  968. }
  969. /* if old and new are the same, this'll just do one lock. */
  970. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  971. &new_dir_bh, new_dir);
  972. if (status < 0) {
  973. mlog_errno(status);
  974. goto bail;
  975. }
  976. parents_locked = 1;
  977. /* make sure both dirs have bhs
  978. * get an extra ref on old_dir_bh if old==new */
  979. if (!new_dir_bh) {
  980. if (old_dir_bh) {
  981. new_dir_bh = old_dir_bh;
  982. get_bh(new_dir_bh);
  983. } else {
  984. mlog(ML_ERROR, "no old_dir_bh!\n");
  985. status = -EIO;
  986. goto bail;
  987. }
  988. }
  989. /*
  990. * Aside from allowing a meta data update, the locking here
  991. * also ensures that the downconvert thread on other nodes
  992. * won't have to concurrently downconvert the inode and the
  993. * dentry locks.
  994. */
  995. status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
  996. OI_LS_PARENT);
  997. if (status < 0) {
  998. if (status != -ENOENT)
  999. mlog_errno(status);
  1000. goto bail;
  1001. }
  1002. old_child_locked = 1;
  1003. status = ocfs2_remote_dentry_delete(old_dentry);
  1004. if (status < 0) {
  1005. mlog_errno(status);
  1006. goto bail;
  1007. }
  1008. if (S_ISDIR(old_inode->i_mode)) {
  1009. u64 old_inode_parent;
  1010. update_dot_dot = 1;
  1011. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  1012. old_inode,
  1013. &old_inode_dot_dot_res);
  1014. if (status) {
  1015. status = -EIO;
  1016. goto bail;
  1017. }
  1018. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  1019. status = -EIO;
  1020. goto bail;
  1021. }
  1022. if (!new_inode && new_dir != old_dir &&
  1023. new_dir->i_nlink >= ocfs2_link_max(osb)) {
  1024. status = -EMLINK;
  1025. goto bail;
  1026. }
  1027. }
  1028. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  1029. old_dentry->d_name.len,
  1030. &old_de_ino);
  1031. if (status) {
  1032. status = -ENOENT;
  1033. goto bail;
  1034. }
  1035. /*
  1036. * Check for inode number is _not_ due to possible IO errors.
  1037. * We might rmdir the source, keep it as pwd of some process
  1038. * and merrily kill the link to whatever was created under the
  1039. * same name. Goodbye sticky bit ;-<
  1040. */
  1041. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  1042. status = -ENOENT;
  1043. goto bail;
  1044. }
  1045. /* check if the target already exists (in which case we need
  1046. * to delete it */
  1047. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  1048. new_dentry->d_name.len,
  1049. &newfe_blkno, new_dir,
  1050. &target_lookup_res);
  1051. /* The only error we allow here is -ENOENT because the new
  1052. * file not existing is perfectly valid. */
  1053. if ((status < 0) && (status != -ENOENT)) {
  1054. /* If we cannot find the file specified we should just */
  1055. /* return the error... */
  1056. mlog_errno(status);
  1057. goto bail;
  1058. }
  1059. if (status == 0)
  1060. target_exists = 1;
  1061. if (!target_exists && new_inode) {
  1062. /*
  1063. * Target was unlinked by another node while we were
  1064. * waiting to get to ocfs2_rename(). There isn't
  1065. * anything we can do here to help the situation, so
  1066. * bubble up the appropriate error.
  1067. */
  1068. status = -ENOENT;
  1069. goto bail;
  1070. }
  1071. /* In case we need to overwrite an existing file, we blow it
  1072. * away first */
  1073. if (target_exists) {
  1074. /* VFS didn't think there existed an inode here, but
  1075. * someone else in the cluster must have raced our
  1076. * rename to create one. Today we error cleanly, in
  1077. * the future we should consider calling iget to build
  1078. * a new struct inode for this entry. */
  1079. if (!new_inode) {
  1080. status = -EACCES;
  1081. trace_ocfs2_rename_target_exists(new_dentry->d_name.len,
  1082. new_dentry->d_name.name);
  1083. goto bail;
  1084. }
  1085. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1086. status = -EACCES;
  1087. trace_ocfs2_rename_disagree(
  1088. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1089. (unsigned long long)newfe_blkno,
  1090. OCFS2_I(new_inode)->ip_flags);
  1091. goto bail;
  1092. }
  1093. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  1094. if (status < 0) {
  1095. if (status != -ENOENT)
  1096. mlog_errno(status);
  1097. goto bail;
  1098. }
  1099. new_child_locked = 1;
  1100. status = ocfs2_remote_dentry_delete(new_dentry);
  1101. if (status < 0) {
  1102. mlog_errno(status);
  1103. goto bail;
  1104. }
  1105. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1106. trace_ocfs2_rename_over_existing(
  1107. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1108. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1109. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1110. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1111. OCFS2_I(new_inode)->ip_blkno,
  1112. orphan_name, &orphan_insert);
  1113. if (status < 0) {
  1114. mlog_errno(status);
  1115. goto bail;
  1116. }
  1117. }
  1118. } else {
  1119. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  1120. status = ocfs2_check_dir_for_entry(new_dir,
  1121. new_dentry->d_name.name,
  1122. new_dentry->d_name.len);
  1123. if (status)
  1124. goto bail;
  1125. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1126. new_dentry->d_name.name,
  1127. new_dentry->d_name.len,
  1128. &target_insert);
  1129. if (status < 0) {
  1130. mlog_errno(status);
  1131. goto bail;
  1132. }
  1133. }
  1134. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1135. if (IS_ERR(handle)) {
  1136. status = PTR_ERR(handle);
  1137. handle = NULL;
  1138. mlog_errno(status);
  1139. goto bail;
  1140. }
  1141. if (target_exists) {
  1142. if (S_ISDIR(new_inode->i_mode)) {
  1143. if (new_inode->i_nlink != 2 ||
  1144. !ocfs2_empty_dir(new_inode)) {
  1145. status = -ENOTEMPTY;
  1146. goto bail;
  1147. }
  1148. }
  1149. status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
  1150. newfe_bh,
  1151. OCFS2_JOURNAL_ACCESS_WRITE);
  1152. if (status < 0) {
  1153. mlog_errno(status);
  1154. goto bail;
  1155. }
  1156. if (S_ISDIR(new_inode->i_mode) ||
  1157. (ocfs2_read_links_count(newfe) == 1)) {
  1158. status = ocfs2_orphan_add(osb, handle, new_inode,
  1159. newfe_bh, orphan_name,
  1160. &orphan_insert, orphan_dir);
  1161. if (status < 0) {
  1162. mlog_errno(status);
  1163. goto bail;
  1164. }
  1165. }
  1166. /* change the dirent to point to the correct inode */
  1167. status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
  1168. old_inode);
  1169. if (status < 0) {
  1170. mlog_errno(status);
  1171. goto bail;
  1172. }
  1173. new_dir->i_version++;
  1174. if (S_ISDIR(new_inode->i_mode))
  1175. ocfs2_set_links_count(newfe, 0);
  1176. else
  1177. ocfs2_add_links_count(newfe, -1);
  1178. ocfs2_journal_dirty(handle, newfe_bh);
  1179. } else {
  1180. /* if the name was not found in new_dir, add it now */
  1181. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1182. OCFS2_I(old_inode)->ip_blkno,
  1183. new_dir_bh, &target_insert);
  1184. }
  1185. old_inode->i_ctime = CURRENT_TIME;
  1186. mark_inode_dirty(old_inode);
  1187. status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
  1188. old_inode_bh,
  1189. OCFS2_JOURNAL_ACCESS_WRITE);
  1190. if (status >= 0) {
  1191. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1192. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1193. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1194. ocfs2_journal_dirty(handle, old_inode_bh);
  1195. } else
  1196. mlog_errno(status);
  1197. /*
  1198. * Now that the name has been added to new_dir, remove the old name.
  1199. *
  1200. * We don't keep any directory entry context around until now
  1201. * because the insert might have changed the type of directory
  1202. * we're dealing with.
  1203. */
  1204. status = ocfs2_find_entry(old_dentry->d_name.name,
  1205. old_dentry->d_name.len, old_dir,
  1206. &old_entry_lookup);
  1207. if (status)
  1208. goto bail;
  1209. status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
  1210. if (status < 0) {
  1211. mlog_errno(status);
  1212. goto bail;
  1213. }
  1214. if (new_inode) {
  1215. drop_nlink(new_inode);
  1216. new_inode->i_ctime = CURRENT_TIME;
  1217. }
  1218. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1219. if (update_dot_dot) {
  1220. status = ocfs2_update_entry(old_inode, handle,
  1221. &old_inode_dot_dot_res, new_dir);
  1222. drop_nlink(old_dir);
  1223. if (new_inode) {
  1224. drop_nlink(new_inode);
  1225. } else {
  1226. inc_nlink(new_dir);
  1227. mark_inode_dirty(new_dir);
  1228. }
  1229. }
  1230. mark_inode_dirty(old_dir);
  1231. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1232. if (new_inode) {
  1233. mark_inode_dirty(new_inode);
  1234. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1235. }
  1236. if (old_dir != new_dir) {
  1237. /* Keep the same times on both directories.*/
  1238. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1239. /*
  1240. * This will also pick up the i_nlink change from the
  1241. * block above.
  1242. */
  1243. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1244. }
  1245. if (old_dir_nlink != old_dir->i_nlink) {
  1246. if (!old_dir_bh) {
  1247. mlog(ML_ERROR, "need to change nlink for old dir "
  1248. "%llu from %d to %d but bh is NULL!\n",
  1249. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1250. (int)old_dir_nlink, old_dir->i_nlink);
  1251. } else {
  1252. struct ocfs2_dinode *fe;
  1253. status = ocfs2_journal_access_di(handle,
  1254. INODE_CACHE(old_dir),
  1255. old_dir_bh,
  1256. OCFS2_JOURNAL_ACCESS_WRITE);
  1257. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1258. ocfs2_set_links_count(fe, old_dir->i_nlink);
  1259. ocfs2_journal_dirty(handle, old_dir_bh);
  1260. }
  1261. }
  1262. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1263. status = 0;
  1264. bail:
  1265. if (rename_lock)
  1266. ocfs2_rename_unlock(osb);
  1267. if (handle)
  1268. ocfs2_commit_trans(osb, handle);
  1269. if (parents_locked)
  1270. ocfs2_double_unlock(old_dir, new_dir);
  1271. if (old_child_locked)
  1272. ocfs2_inode_unlock(old_inode, 1);
  1273. if (new_child_locked)
  1274. ocfs2_inode_unlock(new_inode, 1);
  1275. if (orphan_dir) {
  1276. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1277. ocfs2_inode_unlock(orphan_dir, 1);
  1278. mutex_unlock(&orphan_dir->i_mutex);
  1279. iput(orphan_dir);
  1280. }
  1281. if (new_inode)
  1282. sync_mapping_buffers(old_inode->i_mapping);
  1283. if (new_inode)
  1284. iput(new_inode);
  1285. ocfs2_free_dir_lookup_result(&target_lookup_res);
  1286. ocfs2_free_dir_lookup_result(&old_entry_lookup);
  1287. ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
  1288. ocfs2_free_dir_lookup_result(&orphan_insert);
  1289. ocfs2_free_dir_lookup_result(&target_insert);
  1290. brelse(newfe_bh);
  1291. brelse(old_inode_bh);
  1292. brelse(old_dir_bh);
  1293. brelse(new_dir_bh);
  1294. if (status)
  1295. mlog_errno(status);
  1296. return status;
  1297. }
  1298. /*
  1299. * we expect i_size = strlen(symname). Copy symname into the file
  1300. * data, including the null terminator.
  1301. */
  1302. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1303. handle_t *handle,
  1304. struct inode *inode,
  1305. const char *symname)
  1306. {
  1307. struct buffer_head **bhs = NULL;
  1308. const char *c;
  1309. struct super_block *sb = osb->sb;
  1310. u64 p_blkno, p_blocks;
  1311. int virtual, blocks, status, i, bytes_left;
  1312. bytes_left = i_size_read(inode) + 1;
  1313. /* we can't trust i_blocks because we're actually going to
  1314. * write i_size + 1 bytes. */
  1315. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1316. trace_ocfs2_create_symlink_data((unsigned long long)inode->i_blocks,
  1317. i_size_read(inode), blocks);
  1318. /* Sanity check -- make sure we're going to fit. */
  1319. if (bytes_left >
  1320. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1321. status = -EIO;
  1322. mlog_errno(status);
  1323. goto bail;
  1324. }
  1325. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1326. if (!bhs) {
  1327. status = -ENOMEM;
  1328. mlog_errno(status);
  1329. goto bail;
  1330. }
  1331. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1332. NULL);
  1333. if (status < 0) {
  1334. mlog_errno(status);
  1335. goto bail;
  1336. }
  1337. /* links can never be larger than one cluster so we know this
  1338. * is all going to be contiguous, but do a sanity check
  1339. * anyway. */
  1340. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1341. status = -EIO;
  1342. mlog_errno(status);
  1343. goto bail;
  1344. }
  1345. virtual = 0;
  1346. while(bytes_left > 0) {
  1347. c = &symname[virtual * sb->s_blocksize];
  1348. bhs[virtual] = sb_getblk(sb, p_blkno);
  1349. if (!bhs[virtual]) {
  1350. status = -ENOMEM;
  1351. mlog_errno(status);
  1352. goto bail;
  1353. }
  1354. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
  1355. bhs[virtual]);
  1356. status = ocfs2_journal_access(handle, INODE_CACHE(inode),
  1357. bhs[virtual],
  1358. OCFS2_JOURNAL_ACCESS_CREATE);
  1359. if (status < 0) {
  1360. mlog_errno(status);
  1361. goto bail;
  1362. }
  1363. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1364. memcpy(bhs[virtual]->b_data, c,
  1365. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1366. bytes_left);
  1367. ocfs2_journal_dirty(handle, bhs[virtual]);
  1368. virtual++;
  1369. p_blkno++;
  1370. bytes_left -= sb->s_blocksize;
  1371. }
  1372. status = 0;
  1373. bail:
  1374. if (bhs) {
  1375. for(i = 0; i < blocks; i++)
  1376. brelse(bhs[i]);
  1377. kfree(bhs);
  1378. }
  1379. if (status)
  1380. mlog_errno(status);
  1381. return status;
  1382. }
  1383. static int ocfs2_symlink(struct inode *dir,
  1384. struct dentry *dentry,
  1385. const char *symname)
  1386. {
  1387. int status, l, credits;
  1388. u64 newsize;
  1389. struct ocfs2_super *osb = NULL;
  1390. struct inode *inode = NULL;
  1391. struct super_block *sb;
  1392. struct buffer_head *new_fe_bh = NULL;
  1393. struct buffer_head *parent_fe_bh = NULL;
  1394. struct ocfs2_dinode *fe = NULL;
  1395. struct ocfs2_dinode *dirfe;
  1396. handle_t *handle = NULL;
  1397. struct ocfs2_alloc_context *inode_ac = NULL;
  1398. struct ocfs2_alloc_context *data_ac = NULL;
  1399. struct ocfs2_alloc_context *xattr_ac = NULL;
  1400. int want_clusters = 0;
  1401. int xattr_credits = 0;
  1402. struct ocfs2_security_xattr_info si = {
  1403. .enable = 1,
  1404. };
  1405. int did_quota = 0, did_quota_inode = 0;
  1406. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1407. sigset_t oldset;
  1408. int did_block_signals = 0;
  1409. trace_ocfs2_symlink_begin(dir, dentry, symname,
  1410. dentry->d_name.len, dentry->d_name.name);
  1411. dquot_initialize(dir);
  1412. sb = dir->i_sb;
  1413. osb = OCFS2_SB(sb);
  1414. l = strlen(symname) + 1;
  1415. credits = ocfs2_calc_symlink_credits(sb);
  1416. /* lock the parent directory */
  1417. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1418. if (status < 0) {
  1419. if (status != -ENOENT)
  1420. mlog_errno(status);
  1421. return status;
  1422. }
  1423. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1424. if (!ocfs2_read_links_count(dirfe)) {
  1425. /* can't make a file in a deleted directory. */
  1426. status = -ENOENT;
  1427. goto bail;
  1428. }
  1429. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1430. dentry->d_name.len);
  1431. if (status)
  1432. goto bail;
  1433. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1434. dentry->d_name.name,
  1435. dentry->d_name.len, &lookup);
  1436. if (status < 0) {
  1437. mlog_errno(status);
  1438. goto bail;
  1439. }
  1440. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1441. if (status < 0) {
  1442. if (status != -ENOSPC)
  1443. mlog_errno(status);
  1444. goto bail;
  1445. }
  1446. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1447. if (!inode) {
  1448. status = -ENOMEM;
  1449. mlog_errno(status);
  1450. goto bail;
  1451. }
  1452. /* get security xattr */
  1453. status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
  1454. if (status) {
  1455. if (status == -EOPNOTSUPP)
  1456. si.enable = 0;
  1457. else {
  1458. mlog_errno(status);
  1459. goto bail;
  1460. }
  1461. }
  1462. /* calculate meta data/clusters for setting security xattr */
  1463. if (si.enable) {
  1464. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1465. &xattr_credits, &xattr_ac);
  1466. if (status < 0) {
  1467. mlog_errno(status);
  1468. goto bail;
  1469. }
  1470. }
  1471. /* don't reserve bitmap space for fast symlinks. */
  1472. if (l > ocfs2_fast_symlink_chars(sb))
  1473. want_clusters += 1;
  1474. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1475. if (status < 0) {
  1476. if (status != -ENOSPC)
  1477. mlog_errno(status);
  1478. goto bail;
  1479. }
  1480. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1481. if (IS_ERR(handle)) {
  1482. status = PTR_ERR(handle);
  1483. handle = NULL;
  1484. mlog_errno(status);
  1485. goto bail;
  1486. }
  1487. /* Starting to change things, restart is no longer possible. */
  1488. ocfs2_block_signals(&oldset);
  1489. did_block_signals = 1;
  1490. status = dquot_alloc_inode(inode);
  1491. if (status)
  1492. goto bail;
  1493. did_quota_inode = 1;
  1494. trace_ocfs2_symlink_create(dir, dentry, dentry->d_name.len,
  1495. dentry->d_name.name,
  1496. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  1497. inode->i_mode);
  1498. status = ocfs2_mknod_locked(osb, dir, inode,
  1499. 0, &new_fe_bh, parent_fe_bh, handle,
  1500. inode_ac);
  1501. if (status < 0) {
  1502. mlog_errno(status);
  1503. goto bail;
  1504. }
  1505. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1506. inode->i_rdev = 0;
  1507. newsize = l - 1;
  1508. inode->i_op = &ocfs2_symlink_inode_operations;
  1509. if (l > ocfs2_fast_symlink_chars(sb)) {
  1510. u32 offset = 0;
  1511. status = dquot_alloc_space_nodirty(inode,
  1512. ocfs2_clusters_to_bytes(osb->sb, 1));
  1513. if (status)
  1514. goto bail;
  1515. did_quota = 1;
  1516. inode->i_mapping->a_ops = &ocfs2_aops;
  1517. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1518. new_fe_bh,
  1519. handle, data_ac, NULL,
  1520. NULL);
  1521. if (status < 0) {
  1522. if (status != -ENOSPC && status != -EINTR) {
  1523. mlog(ML_ERROR,
  1524. "Failed to extend file to %llu\n",
  1525. (unsigned long long)newsize);
  1526. mlog_errno(status);
  1527. status = -ENOSPC;
  1528. }
  1529. goto bail;
  1530. }
  1531. i_size_write(inode, newsize);
  1532. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1533. } else {
  1534. inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
  1535. memcpy((char *) fe->id2.i_symlink, symname, l);
  1536. i_size_write(inode, newsize);
  1537. inode->i_blocks = 0;
  1538. }
  1539. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1540. if (status < 0) {
  1541. mlog_errno(status);
  1542. goto bail;
  1543. }
  1544. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1545. status = ocfs2_create_symlink_data(osb, handle, inode,
  1546. symname);
  1547. if (status < 0) {
  1548. mlog_errno(status);
  1549. goto bail;
  1550. }
  1551. }
  1552. if (si.enable) {
  1553. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1554. xattr_ac, data_ac);
  1555. if (status < 0) {
  1556. mlog_errno(status);
  1557. goto bail;
  1558. }
  1559. }
  1560. /*
  1561. * Do this before adding the entry to the directory. We add
  1562. * also set d_op after success so that ->d_iput() will cleanup
  1563. * the dentry lock even if ocfs2_add_entry() fails below.
  1564. */
  1565. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1566. if (status) {
  1567. mlog_errno(status);
  1568. goto bail;
  1569. }
  1570. status = ocfs2_add_entry(handle, dentry, inode,
  1571. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1572. &lookup);
  1573. if (status < 0) {
  1574. mlog_errno(status);
  1575. goto bail;
  1576. }
  1577. insert_inode_hash(inode);
  1578. d_instantiate(dentry, inode);
  1579. bail:
  1580. if (status < 0 && did_quota)
  1581. dquot_free_space_nodirty(inode,
  1582. ocfs2_clusters_to_bytes(osb->sb, 1));
  1583. if (status < 0 && did_quota_inode)
  1584. dquot_free_inode(inode);
  1585. if (handle)
  1586. ocfs2_commit_trans(osb, handle);
  1587. ocfs2_inode_unlock(dir, 1);
  1588. if (did_block_signals)
  1589. ocfs2_unblock_signals(&oldset);
  1590. brelse(new_fe_bh);
  1591. brelse(parent_fe_bh);
  1592. kfree(si.value);
  1593. ocfs2_free_dir_lookup_result(&lookup);
  1594. if (inode_ac)
  1595. ocfs2_free_alloc_context(inode_ac);
  1596. if (data_ac)
  1597. ocfs2_free_alloc_context(data_ac);
  1598. if (xattr_ac)
  1599. ocfs2_free_alloc_context(xattr_ac);
  1600. if ((status < 0) && inode) {
  1601. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
  1602. clear_nlink(inode);
  1603. iput(inode);
  1604. }
  1605. if (status)
  1606. mlog_errno(status);
  1607. return status;
  1608. }
  1609. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1610. {
  1611. int status, namelen;
  1612. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1613. (long long)blkno);
  1614. if (namelen <= 0) {
  1615. if (namelen)
  1616. status = namelen;
  1617. else
  1618. status = -EINVAL;
  1619. mlog_errno(status);
  1620. goto bail;
  1621. }
  1622. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1623. status = -EINVAL;
  1624. mlog_errno(status);
  1625. goto bail;
  1626. }
  1627. trace_ocfs2_blkno_stringify(blkno, name, namelen);
  1628. status = 0;
  1629. bail:
  1630. if (status < 0)
  1631. mlog_errno(status);
  1632. return status;
  1633. }
  1634. static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
  1635. struct inode **ret_orphan_dir,
  1636. struct buffer_head **ret_orphan_dir_bh)
  1637. {
  1638. struct inode *orphan_dir_inode;
  1639. struct buffer_head *orphan_dir_bh = NULL;
  1640. int ret = 0;
  1641. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1642. ORPHAN_DIR_SYSTEM_INODE,
  1643. osb->slot_num);
  1644. if (!orphan_dir_inode) {
  1645. ret = -ENOENT;
  1646. mlog_errno(ret);
  1647. return ret;
  1648. }
  1649. mutex_lock(&orphan_dir_inode->i_mutex);
  1650. ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1651. if (ret < 0) {
  1652. mutex_unlock(&orphan_dir_inode->i_mutex);
  1653. iput(orphan_dir_inode);
  1654. mlog_errno(ret);
  1655. return ret;
  1656. }
  1657. *ret_orphan_dir = orphan_dir_inode;
  1658. *ret_orphan_dir_bh = orphan_dir_bh;
  1659. return 0;
  1660. }
  1661. static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode,
  1662. struct buffer_head *orphan_dir_bh,
  1663. u64 blkno,
  1664. char *name,
  1665. struct ocfs2_dir_lookup_result *lookup)
  1666. {
  1667. int ret;
  1668. struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb);
  1669. ret = ocfs2_blkno_stringify(blkno, name);
  1670. if (ret < 0) {
  1671. mlog_errno(ret);
  1672. return ret;
  1673. }
  1674. ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1675. orphan_dir_bh, name,
  1676. OCFS2_ORPHAN_NAMELEN, lookup);
  1677. if (ret < 0) {
  1678. mlog_errno(ret);
  1679. return ret;
  1680. }
  1681. return 0;
  1682. }
  1683. /**
  1684. * ocfs2_prepare_orphan_dir() - Prepare an orphan directory for
  1685. * insertion of an orphan.
  1686. * @osb: ocfs2 file system
  1687. * @ret_orphan_dir: Orphan dir inode - returned locked!
  1688. * @blkno: Actual block number of the inode to be inserted into orphan dir.
  1689. * @lookup: dir lookup result, to be passed back into functions like
  1690. * ocfs2_orphan_add
  1691. *
  1692. * Returns zero on success and the ret_orphan_dir, name and lookup
  1693. * fields will be populated.
  1694. *
  1695. * Returns non-zero on failure.
  1696. */
  1697. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1698. struct inode **ret_orphan_dir,
  1699. u64 blkno,
  1700. char *name,
  1701. struct ocfs2_dir_lookup_result *lookup)
  1702. {
  1703. struct inode *orphan_dir_inode = NULL;
  1704. struct buffer_head *orphan_dir_bh = NULL;
  1705. int ret = 0;
  1706. ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
  1707. &orphan_dir_bh);
  1708. if (ret < 0) {
  1709. mlog_errno(ret);
  1710. return ret;
  1711. }
  1712. ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
  1713. blkno, name, lookup);
  1714. if (ret < 0) {
  1715. mlog_errno(ret);
  1716. goto out;
  1717. }
  1718. *ret_orphan_dir = orphan_dir_inode;
  1719. out:
  1720. brelse(orphan_dir_bh);
  1721. if (ret) {
  1722. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1723. mutex_unlock(&orphan_dir_inode->i_mutex);
  1724. iput(orphan_dir_inode);
  1725. }
  1726. if (ret)
  1727. mlog_errno(ret);
  1728. return ret;
  1729. }
  1730. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1731. handle_t *handle,
  1732. struct inode *inode,
  1733. struct buffer_head *fe_bh,
  1734. char *name,
  1735. struct ocfs2_dir_lookup_result *lookup,
  1736. struct inode *orphan_dir_inode)
  1737. {
  1738. struct buffer_head *orphan_dir_bh = NULL;
  1739. int status = 0;
  1740. struct ocfs2_dinode *orphan_fe;
  1741. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  1742. trace_ocfs2_orphan_add_begin(
  1743. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1744. status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
  1745. if (status < 0) {
  1746. mlog_errno(status);
  1747. goto leave;
  1748. }
  1749. status = ocfs2_journal_access_di(handle,
  1750. INODE_CACHE(orphan_dir_inode),
  1751. orphan_dir_bh,
  1752. OCFS2_JOURNAL_ACCESS_WRITE);
  1753. if (status < 0) {
  1754. mlog_errno(status);
  1755. goto leave;
  1756. }
  1757. /*
  1758. * We're going to journal the change of i_flags and i_orphaned_slot.
  1759. * It's safe anyway, though some callers may duplicate the journaling.
  1760. * Journaling within the func just make the logic look more
  1761. * straightforward.
  1762. */
  1763. status = ocfs2_journal_access_di(handle,
  1764. INODE_CACHE(inode),
  1765. fe_bh,
  1766. OCFS2_JOURNAL_ACCESS_WRITE);
  1767. if (status < 0) {
  1768. mlog_errno(status);
  1769. goto leave;
  1770. }
  1771. /* we're a cluster, and nlink can change on disk from
  1772. * underneath us... */
  1773. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1774. if (S_ISDIR(inode->i_mode))
  1775. ocfs2_add_links_count(orphan_fe, 1);
  1776. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1777. ocfs2_journal_dirty(handle, orphan_dir_bh);
  1778. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1779. OCFS2_ORPHAN_NAMELEN, inode,
  1780. OCFS2_I(inode)->ip_blkno,
  1781. orphan_dir_bh, lookup);
  1782. if (status < 0) {
  1783. mlog_errno(status);
  1784. goto rollback;
  1785. }
  1786. fe->i_flags |= cpu_to_le32(OCFS2_ORPHANED_FL);
  1787. OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
  1788. /* Record which orphan dir our inode now resides
  1789. * in. delete_inode will use this to determine which orphan
  1790. * dir to lock. */
  1791. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1792. ocfs2_journal_dirty(handle, fe_bh);
  1793. trace_ocfs2_orphan_add_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
  1794. osb->slot_num);
  1795. rollback:
  1796. if (status < 0) {
  1797. if (S_ISDIR(inode->i_mode))
  1798. ocfs2_add_links_count(orphan_fe, -1);
  1799. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1800. }
  1801. leave:
  1802. brelse(orphan_dir_bh);
  1803. return status;
  1804. }
  1805. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1806. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1807. handle_t *handle,
  1808. struct inode *orphan_dir_inode,
  1809. struct inode *inode,
  1810. struct buffer_head *orphan_dir_bh)
  1811. {
  1812. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1813. struct ocfs2_dinode *orphan_fe;
  1814. int status = 0;
  1815. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1816. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1817. if (status < 0) {
  1818. mlog_errno(status);
  1819. goto leave;
  1820. }
  1821. trace_ocfs2_orphan_del(
  1822. (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1823. name, OCFS2_ORPHAN_NAMELEN);
  1824. /* find it's spot in the orphan directory */
  1825. status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
  1826. &lookup);
  1827. if (status) {
  1828. mlog_errno(status);
  1829. goto leave;
  1830. }
  1831. /* remove it from the orphan directory */
  1832. status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
  1833. if (status < 0) {
  1834. mlog_errno(status);
  1835. goto leave;
  1836. }
  1837. status = ocfs2_journal_access_di(handle,
  1838. INODE_CACHE(orphan_dir_inode),
  1839. orphan_dir_bh,
  1840. OCFS2_JOURNAL_ACCESS_WRITE);
  1841. if (status < 0) {
  1842. mlog_errno(status);
  1843. goto leave;
  1844. }
  1845. /* do the i_nlink dance! :) */
  1846. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1847. if (S_ISDIR(inode->i_mode))
  1848. ocfs2_add_links_count(orphan_fe, -1);
  1849. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1850. ocfs2_journal_dirty(handle, orphan_dir_bh);
  1851. leave:
  1852. ocfs2_free_dir_lookup_result(&lookup);
  1853. if (status)
  1854. mlog_errno(status);
  1855. return status;
  1856. }
  1857. /**
  1858. * ocfs2_prep_new_orphaned_file() - Prepare the orphan dir to receive a newly
  1859. * allocated file. This is different from the typical 'add to orphan dir'
  1860. * operation in that the inode does not yet exist. This is a problem because
  1861. * the orphan dir stringifies the inode block number to come up with it's
  1862. * dirent. Obviously if the inode does not yet exist we have a chicken and egg
  1863. * problem. This function works around it by calling deeper into the orphan
  1864. * and suballoc code than other callers. Use this only by necessity.
  1865. * @dir: The directory which this inode will ultimately wind up under - not the
  1866. * orphan dir!
  1867. * @dir_bh: buffer_head the @dir inode block
  1868. * @orphan_name: string of length (CFS2_ORPHAN_NAMELEN + 1). Will be filled
  1869. * with the string to be used for orphan dirent. Pass back to the orphan dir
  1870. * code.
  1871. * @ret_orphan_dir: orphan dir inode returned to be passed back into orphan
  1872. * dir code.
  1873. * @ret_di_blkno: block number where the new inode will be allocated.
  1874. * @orphan_insert: Dir insert context to be passed back into orphan dir code.
  1875. * @ret_inode_ac: Inode alloc context to be passed back to the allocator.
  1876. *
  1877. * Returns zero on success and the ret_orphan_dir, name and lookup
  1878. * fields will be populated.
  1879. *
  1880. * Returns non-zero on failure.
  1881. */
  1882. static int ocfs2_prep_new_orphaned_file(struct inode *dir,
  1883. struct buffer_head *dir_bh,
  1884. char *orphan_name,
  1885. struct inode **ret_orphan_dir,
  1886. u64 *ret_di_blkno,
  1887. struct ocfs2_dir_lookup_result *orphan_insert,
  1888. struct ocfs2_alloc_context **ret_inode_ac)
  1889. {
  1890. int ret;
  1891. u64 di_blkno;
  1892. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1893. struct inode *orphan_dir = NULL;
  1894. struct buffer_head *orphan_dir_bh = NULL;
  1895. struct ocfs2_alloc_context *inode_ac = NULL;
  1896. ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
  1897. if (ret < 0) {
  1898. mlog_errno(ret);
  1899. return ret;
  1900. }
  1901. /* reserve an inode spot */
  1902. ret = ocfs2_reserve_new_inode(osb, &inode_ac);
  1903. if (ret < 0) {
  1904. if (ret != -ENOSPC)
  1905. mlog_errno(ret);
  1906. goto out;
  1907. }
  1908. ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac,
  1909. &di_blkno);
  1910. if (ret) {
  1911. mlog_errno(ret);
  1912. goto out;
  1913. }
  1914. ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
  1915. di_blkno, orphan_name, orphan_insert);
  1916. if (ret < 0) {
  1917. mlog_errno(ret);
  1918. goto out;
  1919. }
  1920. out:
  1921. if (ret == 0) {
  1922. *ret_orphan_dir = orphan_dir;
  1923. *ret_di_blkno = di_blkno;
  1924. *ret_inode_ac = inode_ac;
  1925. /*
  1926. * orphan_name and orphan_insert are already up to
  1927. * date via prepare_orphan_dir
  1928. */
  1929. } else {
  1930. /* Unroll reserve_new_inode* */
  1931. if (inode_ac)
  1932. ocfs2_free_alloc_context(inode_ac);
  1933. /* Unroll orphan dir locking */
  1934. mutex_unlock(&orphan_dir->i_mutex);
  1935. ocfs2_inode_unlock(orphan_dir, 1);
  1936. iput(orphan_dir);
  1937. }
  1938. brelse(orphan_dir_bh);
  1939. return ret;
  1940. }
  1941. int ocfs2_create_inode_in_orphan(struct inode *dir,
  1942. int mode,
  1943. struct inode **new_inode)
  1944. {
  1945. int status, did_quota_inode = 0;
  1946. struct inode *inode = NULL;
  1947. struct inode *orphan_dir = NULL;
  1948. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1949. struct ocfs2_dinode *di = NULL;
  1950. handle_t *handle = NULL;
  1951. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  1952. struct buffer_head *parent_di_bh = NULL;
  1953. struct buffer_head *new_di_bh = NULL;
  1954. struct ocfs2_alloc_context *inode_ac = NULL;
  1955. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  1956. u64 uninitialized_var(di_blkno), suballoc_loc;
  1957. u16 suballoc_bit;
  1958. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  1959. if (status < 0) {
  1960. if (status != -ENOENT)
  1961. mlog_errno(status);
  1962. return status;
  1963. }
  1964. status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
  1965. orphan_name, &orphan_dir,
  1966. &di_blkno, &orphan_insert, &inode_ac);
  1967. if (status < 0) {
  1968. if (status != -ENOSPC)
  1969. mlog_errno(status);
  1970. goto leave;
  1971. }
  1972. inode = ocfs2_get_init_inode(dir, mode);
  1973. if (!inode) {
  1974. status = -ENOMEM;
  1975. mlog_errno(status);
  1976. goto leave;
  1977. }
  1978. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
  1979. if (IS_ERR(handle)) {
  1980. status = PTR_ERR(handle);
  1981. handle = NULL;
  1982. mlog_errno(status);
  1983. goto leave;
  1984. }
  1985. status = dquot_alloc_inode(inode);
  1986. if (status)
  1987. goto leave;
  1988. did_quota_inode = 1;
  1989. status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
  1990. &suballoc_loc,
  1991. &suballoc_bit, di_blkno);
  1992. if (status < 0) {
  1993. mlog_errno(status);
  1994. goto leave;
  1995. }
  1996. clear_nlink(inode);
  1997. /* do the real work now. */
  1998. status = __ocfs2_mknod_locked(dir, inode,
  1999. 0, &new_di_bh, parent_di_bh, handle,
  2000. inode_ac, di_blkno, suballoc_loc,
  2001. suballoc_bit);
  2002. if (status < 0) {
  2003. mlog_errno(status);
  2004. goto leave;
  2005. }
  2006. di = (struct ocfs2_dinode *)new_di_bh->b_data;
  2007. status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
  2008. &orphan_insert, orphan_dir);
  2009. if (status < 0) {
  2010. mlog_errno(status);
  2011. goto leave;
  2012. }
  2013. /* get open lock so that only nodes can't remove it from orphan dir. */
  2014. status = ocfs2_open_lock(inode);
  2015. if (status < 0)
  2016. mlog_errno(status);
  2017. insert_inode_hash(inode);
  2018. leave:
  2019. if (status < 0 && did_quota_inode)
  2020. dquot_free_inode(inode);
  2021. if (handle)
  2022. ocfs2_commit_trans(osb, handle);
  2023. if (orphan_dir) {
  2024. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  2025. ocfs2_inode_unlock(orphan_dir, 1);
  2026. mutex_unlock(&orphan_dir->i_mutex);
  2027. iput(orphan_dir);
  2028. }
  2029. if ((status < 0) && inode) {
  2030. clear_nlink(inode);
  2031. iput(inode);
  2032. }
  2033. if (inode_ac)
  2034. ocfs2_free_alloc_context(inode_ac);
  2035. brelse(new_di_bh);
  2036. if (!status)
  2037. *new_inode = inode;
  2038. ocfs2_free_dir_lookup_result(&orphan_insert);
  2039. ocfs2_inode_unlock(dir, 1);
  2040. brelse(parent_di_bh);
  2041. return status;
  2042. }
  2043. int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
  2044. struct inode *inode,
  2045. struct dentry *dentry)
  2046. {
  2047. int status = 0;
  2048. struct buffer_head *parent_di_bh = NULL;
  2049. handle_t *handle = NULL;
  2050. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2051. struct ocfs2_dinode *dir_di, *di;
  2052. struct inode *orphan_dir_inode = NULL;
  2053. struct buffer_head *orphan_dir_bh = NULL;
  2054. struct buffer_head *di_bh = NULL;
  2055. struct ocfs2_dir_lookup_result lookup = { NULL, };
  2056. trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry,
  2057. dentry->d_name.len, dentry->d_name.name,
  2058. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2059. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2060. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  2061. if (status < 0) {
  2062. if (status != -ENOENT)
  2063. mlog_errno(status);
  2064. return status;
  2065. }
  2066. dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
  2067. if (!dir_di->i_links_count) {
  2068. /* can't make a file in a deleted directory. */
  2069. status = -ENOENT;
  2070. goto leave;
  2071. }
  2072. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  2073. dentry->d_name.len);
  2074. if (status)
  2075. goto leave;
  2076. /* get a spot inside the dir. */
  2077. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
  2078. dentry->d_name.name,
  2079. dentry->d_name.len, &lookup);
  2080. if (status < 0) {
  2081. mlog_errno(status);
  2082. goto leave;
  2083. }
  2084. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  2085. ORPHAN_DIR_SYSTEM_INODE,
  2086. osb->slot_num);
  2087. if (!orphan_dir_inode) {
  2088. status = -EEXIST;
  2089. mlog_errno(status);
  2090. goto leave;
  2091. }
  2092. mutex_lock(&orphan_dir_inode->i_mutex);
  2093. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  2094. if (status < 0) {
  2095. mlog_errno(status);
  2096. mutex_unlock(&orphan_dir_inode->i_mutex);
  2097. iput(orphan_dir_inode);
  2098. goto leave;
  2099. }
  2100. status = ocfs2_read_inode_block(inode, &di_bh);
  2101. if (status < 0) {
  2102. mlog_errno(status);
  2103. goto orphan_unlock;
  2104. }
  2105. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  2106. if (IS_ERR(handle)) {
  2107. status = PTR_ERR(handle);
  2108. handle = NULL;
  2109. mlog_errno(status);
  2110. goto orphan_unlock;
  2111. }
  2112. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  2113. di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  2114. if (status < 0) {
  2115. mlog_errno(status);
  2116. goto out_commit;
  2117. }
  2118. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  2119. orphan_dir_bh);
  2120. if (status < 0) {
  2121. mlog_errno(status);
  2122. goto out_commit;
  2123. }
  2124. di = (struct ocfs2_dinode *)di_bh->b_data;
  2125. di->i_flags &= ~cpu_to_le32(OCFS2_ORPHANED_FL);
  2126. di->i_orphaned_slot = 0;
  2127. set_nlink(inode, 1);
  2128. ocfs2_set_links_count(di, inode->i_nlink);
  2129. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  2130. ocfs2_journal_dirty(handle, di_bh);
  2131. status = ocfs2_add_entry(handle, dentry, inode,
  2132. OCFS2_I(inode)->ip_blkno, parent_di_bh,
  2133. &lookup);
  2134. if (status < 0) {
  2135. mlog_errno(status);
  2136. goto out_commit;
  2137. }
  2138. status = ocfs2_dentry_attach_lock(dentry, inode,
  2139. OCFS2_I(dir)->ip_blkno);
  2140. if (status) {
  2141. mlog_errno(status);
  2142. goto out_commit;
  2143. }
  2144. d_instantiate(dentry, inode);
  2145. status = 0;
  2146. out_commit:
  2147. ocfs2_commit_trans(osb, handle);
  2148. orphan_unlock:
  2149. ocfs2_inode_unlock(orphan_dir_inode, 1);
  2150. mutex_unlock(&orphan_dir_inode->i_mutex);
  2151. iput(orphan_dir_inode);
  2152. leave:
  2153. ocfs2_inode_unlock(dir, 1);
  2154. brelse(di_bh);
  2155. brelse(parent_di_bh);
  2156. brelse(orphan_dir_bh);
  2157. ocfs2_free_dir_lookup_result(&lookup);
  2158. if (status)
  2159. mlog_errno(status);
  2160. return status;
  2161. }
  2162. const struct inode_operations ocfs2_dir_iops = {
  2163. .create = ocfs2_create,
  2164. .lookup = ocfs2_lookup,
  2165. .link = ocfs2_link,
  2166. .unlink = ocfs2_unlink,
  2167. .rmdir = ocfs2_unlink,
  2168. .symlink = ocfs2_symlink,
  2169. .mkdir = ocfs2_mkdir,
  2170. .mknod = ocfs2_mknod,
  2171. .rename = ocfs2_rename,
  2172. .setattr = ocfs2_setattr,
  2173. .getattr = ocfs2_getattr,
  2174. .permission = ocfs2_permission,
  2175. .setxattr = generic_setxattr,
  2176. .getxattr = generic_getxattr,
  2177. .listxattr = ocfs2_listxattr,
  2178. .removexattr = generic_removexattr,
  2179. .fiemap = ocfs2_fiemap,
  2180. .get_acl = ocfs2_iop_get_acl,
  2181. .set_acl = ocfs2_iop_set_acl,
  2182. };