namei.c 54 KB

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