namei.c 56 KB

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