namei.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  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. /* We don't use standard VFS wrapper because we don't want vfs_dq_init
  306. * to be called. */
  307. if (sb_any_quota_active(osb->sb) &&
  308. osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
  309. status = -EDQUOT;
  310. goto leave;
  311. }
  312. did_quota_inode = 1;
  313. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
  314. inode->i_mode, (unsigned long)dev, dentry->d_name.len,
  315. dentry->d_name.name);
  316. /* do the real work now. */
  317. status = ocfs2_mknod_locked(osb, dir, inode, dev,
  318. &new_fe_bh, parent_fe_bh, handle,
  319. inode_ac);
  320. if (status < 0) {
  321. mlog_errno(status);
  322. goto leave;
  323. }
  324. if (S_ISDIR(mode)) {
  325. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  326. new_fe_bh, data_ac, meta_ac);
  327. if (status < 0) {
  328. mlog_errno(status);
  329. goto leave;
  330. }
  331. status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
  332. parent_fe_bh,
  333. OCFS2_JOURNAL_ACCESS_WRITE);
  334. if (status < 0) {
  335. mlog_errno(status);
  336. goto leave;
  337. }
  338. ocfs2_add_links_count(dirfe, 1);
  339. status = ocfs2_journal_dirty(handle, parent_fe_bh);
  340. if (status < 0) {
  341. mlog_errno(status);
  342. goto leave;
  343. }
  344. inc_nlink(dir);
  345. }
  346. status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
  347. meta_ac, data_ac);
  348. if (status < 0) {
  349. mlog_errno(status);
  350. goto leave;
  351. }
  352. if (si.enable) {
  353. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  354. meta_ac, data_ac);
  355. if (status < 0) {
  356. mlog_errno(status);
  357. goto leave;
  358. }
  359. }
  360. status = ocfs2_add_entry(handle, dentry, inode,
  361. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  362. &lookup);
  363. if (status < 0) {
  364. mlog_errno(status);
  365. goto leave;
  366. }
  367. status = ocfs2_dentry_attach_lock(dentry, inode,
  368. OCFS2_I(dir)->ip_blkno);
  369. if (status) {
  370. mlog_errno(status);
  371. goto leave;
  372. }
  373. insert_inode_hash(inode);
  374. dentry->d_op = &ocfs2_dentry_ops;
  375. d_instantiate(dentry, inode);
  376. status = 0;
  377. leave:
  378. if (status < 0 && did_quota_inode)
  379. vfs_dq_free_inode(inode);
  380. if (handle)
  381. ocfs2_commit_trans(osb, handle);
  382. ocfs2_inode_unlock(dir, 1);
  383. if (status == -ENOSPC)
  384. mlog(0, "Disk is full\n");
  385. brelse(new_fe_bh);
  386. brelse(parent_fe_bh);
  387. kfree(si.name);
  388. kfree(si.value);
  389. ocfs2_free_dir_lookup_result(&lookup);
  390. if ((status < 0) && inode) {
  391. clear_nlink(inode);
  392. iput(inode);
  393. }
  394. if (inode_ac)
  395. ocfs2_free_alloc_context(inode_ac);
  396. if (data_ac)
  397. ocfs2_free_alloc_context(data_ac);
  398. if (meta_ac)
  399. ocfs2_free_alloc_context(meta_ac);
  400. mlog_exit(status);
  401. return status;
  402. }
  403. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  404. struct inode *dir,
  405. struct inode *inode,
  406. dev_t dev,
  407. struct buffer_head **new_fe_bh,
  408. struct buffer_head *parent_fe_bh,
  409. handle_t *handle,
  410. struct ocfs2_alloc_context *inode_ac)
  411. {
  412. int status = 0;
  413. struct ocfs2_dinode *fe = NULL;
  414. struct ocfs2_extent_list *fel;
  415. u64 fe_blkno = 0;
  416. u16 suballoc_bit;
  417. u16 feat;
  418. *new_fe_bh = NULL;
  419. status = ocfs2_claim_new_inode(osb, handle, dir, parent_fe_bh,
  420. inode_ac, &suballoc_bit, &fe_blkno);
  421. if (status < 0) {
  422. mlog_errno(status);
  423. goto leave;
  424. }
  425. /* populate as many fields early on as possible - many of
  426. * these are used by the support functions here and in
  427. * callers. */
  428. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  429. OCFS2_I(inode)->ip_blkno = fe_blkno;
  430. spin_lock(&osb->osb_lock);
  431. inode->i_generation = osb->s_next_generation++;
  432. spin_unlock(&osb->osb_lock);
  433. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  434. if (!*new_fe_bh) {
  435. status = -EIO;
  436. mlog_errno(status);
  437. goto leave;
  438. }
  439. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
  440. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  441. *new_fe_bh,
  442. OCFS2_JOURNAL_ACCESS_CREATE);
  443. if (status < 0) {
  444. mlog_errno(status);
  445. goto leave;
  446. }
  447. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  448. memset(fe, 0, osb->sb->s_blocksize);
  449. fe->i_generation = cpu_to_le32(inode->i_generation);
  450. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  451. fe->i_blkno = cpu_to_le64(fe_blkno);
  452. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  453. fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
  454. fe->i_uid = cpu_to_le32(inode->i_uid);
  455. fe->i_gid = cpu_to_le32(inode->i_gid);
  456. fe->i_mode = cpu_to_le16(inode->i_mode);
  457. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  458. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  459. ocfs2_set_links_count(fe, inode->i_nlink);
  460. fe->i_last_eb_blk = 0;
  461. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  462. le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
  463. fe->i_atime = fe->i_ctime = fe->i_mtime =
  464. cpu_to_le64(CURRENT_TIME.tv_sec);
  465. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  466. cpu_to_le32(CURRENT_TIME.tv_nsec);
  467. fe->i_dtime = 0;
  468. /*
  469. * If supported, directories start with inline data. If inline
  470. * isn't supported, but indexing is, we start them as indexed.
  471. */
  472. feat = le16_to_cpu(fe->i_dyn_features);
  473. if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
  474. fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
  475. fe->id2.i_data.id_count = cpu_to_le16(
  476. ocfs2_max_inline_data_with_xattr(osb->sb, fe));
  477. } else {
  478. fel = &fe->id2.i_list;
  479. fel->l_tree_depth = 0;
  480. fel->l_next_free_rec = 0;
  481. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  482. }
  483. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  484. if (status < 0) {
  485. mlog_errno(status);
  486. goto leave;
  487. }
  488. ocfs2_populate_inode(inode, fe, 1);
  489. ocfs2_ci_set_new(osb, INODE_CACHE(inode));
  490. if (!ocfs2_mount_local(osb)) {
  491. status = ocfs2_create_new_inode_locks(inode);
  492. if (status < 0)
  493. mlog_errno(status);
  494. }
  495. status = 0; /* error in ocfs2_create_new_inode_locks is not
  496. * critical */
  497. leave:
  498. if (status < 0) {
  499. if (*new_fe_bh) {
  500. brelse(*new_fe_bh);
  501. *new_fe_bh = NULL;
  502. }
  503. }
  504. mlog_exit(status);
  505. return status;
  506. }
  507. static int ocfs2_mkdir(struct inode *dir,
  508. struct dentry *dentry,
  509. int mode)
  510. {
  511. int ret;
  512. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  513. dentry->d_name.len, dentry->d_name.name);
  514. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  515. mlog_exit(ret);
  516. return ret;
  517. }
  518. static int ocfs2_create(struct inode *dir,
  519. struct dentry *dentry,
  520. int mode,
  521. struct nameidata *nd)
  522. {
  523. int ret;
  524. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  525. dentry->d_name.len, dentry->d_name.name);
  526. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  527. mlog_exit(ret);
  528. return ret;
  529. }
  530. static int ocfs2_link(struct dentry *old_dentry,
  531. struct inode *dir,
  532. struct dentry *dentry)
  533. {
  534. handle_t *handle;
  535. struct inode *inode = old_dentry->d_inode;
  536. int err;
  537. struct buffer_head *fe_bh = NULL;
  538. struct buffer_head *parent_fe_bh = NULL;
  539. struct ocfs2_dinode *fe = NULL;
  540. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  541. struct ocfs2_dir_lookup_result lookup = { NULL, };
  542. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  543. old_dentry->d_name.len, old_dentry->d_name.name,
  544. dentry->d_name.len, dentry->d_name.name);
  545. if (S_ISDIR(inode->i_mode))
  546. return -EPERM;
  547. err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
  548. if (err < 0) {
  549. if (err != -ENOENT)
  550. mlog_errno(err);
  551. return err;
  552. }
  553. if (!dir->i_nlink) {
  554. err = -ENOENT;
  555. goto out;
  556. }
  557. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  558. dentry->d_name.len);
  559. if (err)
  560. goto out;
  561. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  562. dentry->d_name.name,
  563. dentry->d_name.len, &lookup);
  564. if (err < 0) {
  565. mlog_errno(err);
  566. goto out;
  567. }
  568. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  569. if (err < 0) {
  570. if (err != -ENOENT)
  571. mlog_errno(err);
  572. goto out;
  573. }
  574. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  575. if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
  576. err = -EMLINK;
  577. goto out_unlock_inode;
  578. }
  579. handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
  580. if (IS_ERR(handle)) {
  581. err = PTR_ERR(handle);
  582. handle = NULL;
  583. mlog_errno(err);
  584. goto out_unlock_inode;
  585. }
  586. err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  587. OCFS2_JOURNAL_ACCESS_WRITE);
  588. if (err < 0) {
  589. mlog_errno(err);
  590. goto out_commit;
  591. }
  592. inc_nlink(inode);
  593. inode->i_ctime = CURRENT_TIME;
  594. ocfs2_set_links_count(fe, inode->i_nlink);
  595. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  596. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  597. err = ocfs2_journal_dirty(handle, fe_bh);
  598. if (err < 0) {
  599. ocfs2_add_links_count(fe, -1);
  600. drop_nlink(inode);
  601. mlog_errno(err);
  602. goto out_commit;
  603. }
  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. out_unlock_inode:
  624. ocfs2_inode_unlock(inode, 1);
  625. out:
  626. ocfs2_inode_unlock(dir, 1);
  627. brelse(fe_bh);
  628. brelse(parent_fe_bh);
  629. ocfs2_free_dir_lookup_result(&lookup);
  630. mlog_exit(err);
  631. return err;
  632. }
  633. /*
  634. * Takes and drops an exclusive lock on the given dentry. This will
  635. * force other nodes to drop it.
  636. */
  637. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  638. {
  639. int ret;
  640. ret = ocfs2_dentry_lock(dentry, 1);
  641. if (ret)
  642. mlog_errno(ret);
  643. else
  644. ocfs2_dentry_unlock(dentry, 1);
  645. return ret;
  646. }
  647. static inline int inode_is_unlinkable(struct inode *inode)
  648. {
  649. if (S_ISDIR(inode->i_mode)) {
  650. if (inode->i_nlink == 2)
  651. return 1;
  652. return 0;
  653. }
  654. if (inode->i_nlink == 1)
  655. return 1;
  656. return 0;
  657. }
  658. static int ocfs2_unlink(struct inode *dir,
  659. struct dentry *dentry)
  660. {
  661. int status;
  662. int child_locked = 0;
  663. struct inode *inode = dentry->d_inode;
  664. struct inode *orphan_dir = NULL;
  665. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  666. u64 blkno;
  667. struct ocfs2_dinode *fe = NULL;
  668. struct buffer_head *fe_bh = NULL;
  669. struct buffer_head *parent_node_bh = NULL;
  670. handle_t *handle = NULL;
  671. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  672. struct ocfs2_dir_lookup_result lookup = { NULL, };
  673. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  674. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  675. dentry->d_name.len, dentry->d_name.name);
  676. BUG_ON(dentry->d_parent->d_inode != dir);
  677. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  678. if (inode == osb->root_inode) {
  679. mlog(0, "Cannot delete the root directory\n");
  680. return -EPERM;
  681. }
  682. status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
  683. OI_LS_PARENT);
  684. if (status < 0) {
  685. if (status != -ENOENT)
  686. mlog_errno(status);
  687. return status;
  688. }
  689. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  690. dentry->d_name.len, &blkno, dir,
  691. &lookup);
  692. if (status < 0) {
  693. if (status != -ENOENT)
  694. mlog_errno(status);
  695. goto leave;
  696. }
  697. if (OCFS2_I(inode)->ip_blkno != blkno) {
  698. status = -ENOENT;
  699. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  700. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  701. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  702. goto leave;
  703. }
  704. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  705. if (status < 0) {
  706. if (status != -ENOENT)
  707. mlog_errno(status);
  708. goto leave;
  709. }
  710. child_locked = 1;
  711. if (S_ISDIR(inode->i_mode)) {
  712. if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
  713. status = -ENOTEMPTY;
  714. goto leave;
  715. }
  716. }
  717. status = ocfs2_remote_dentry_delete(dentry);
  718. if (status < 0) {
  719. /* This remote delete should succeed under all normal
  720. * circumstances. */
  721. mlog_errno(status);
  722. goto leave;
  723. }
  724. if (inode_is_unlinkable(inode)) {
  725. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  726. OCFS2_I(inode)->ip_blkno,
  727. orphan_name, &orphan_insert);
  728. if (status < 0) {
  729. mlog_errno(status);
  730. goto leave;
  731. }
  732. }
  733. handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
  734. if (IS_ERR(handle)) {
  735. status = PTR_ERR(handle);
  736. handle = NULL;
  737. mlog_errno(status);
  738. goto leave;
  739. }
  740. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  741. OCFS2_JOURNAL_ACCESS_WRITE);
  742. if (status < 0) {
  743. mlog_errno(status);
  744. goto leave;
  745. }
  746. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  747. if (inode_is_unlinkable(inode)) {
  748. status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
  749. &orphan_insert, orphan_dir);
  750. if (status < 0) {
  751. mlog_errno(status);
  752. goto leave;
  753. }
  754. }
  755. /* delete the name from the parent dir */
  756. status = ocfs2_delete_entry(handle, dir, &lookup);
  757. if (status < 0) {
  758. mlog_errno(status);
  759. goto leave;
  760. }
  761. if (S_ISDIR(inode->i_mode))
  762. drop_nlink(inode);
  763. drop_nlink(inode);
  764. ocfs2_set_links_count(fe, inode->i_nlink);
  765. status = ocfs2_journal_dirty(handle, fe_bh);
  766. if (status < 0) {
  767. mlog_errno(status);
  768. goto leave;
  769. }
  770. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  771. if (S_ISDIR(inode->i_mode))
  772. drop_nlink(dir);
  773. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  774. if (status < 0) {
  775. mlog_errno(status);
  776. if (S_ISDIR(inode->i_mode))
  777. inc_nlink(dir);
  778. }
  779. leave:
  780. if (handle)
  781. ocfs2_commit_trans(osb, handle);
  782. if (child_locked)
  783. ocfs2_inode_unlock(inode, 1);
  784. ocfs2_inode_unlock(dir, 1);
  785. if (orphan_dir) {
  786. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  787. ocfs2_inode_unlock(orphan_dir, 1);
  788. mutex_unlock(&orphan_dir->i_mutex);
  789. iput(orphan_dir);
  790. }
  791. brelse(fe_bh);
  792. brelse(parent_node_bh);
  793. ocfs2_free_dir_lookup_result(&orphan_insert);
  794. ocfs2_free_dir_lookup_result(&lookup);
  795. mlog_exit(status);
  796. return status;
  797. }
  798. /*
  799. * The only place this should be used is rename!
  800. * if they have the same id, then the 1st one is the only one locked.
  801. */
  802. static int ocfs2_double_lock(struct ocfs2_super *osb,
  803. struct buffer_head **bh1,
  804. struct inode *inode1,
  805. struct buffer_head **bh2,
  806. struct inode *inode2)
  807. {
  808. int status;
  809. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  810. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  811. struct buffer_head **tmpbh;
  812. struct inode *tmpinode;
  813. mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
  814. (unsigned long long)oi1->ip_blkno,
  815. (unsigned long long)oi2->ip_blkno);
  816. if (*bh1)
  817. *bh1 = NULL;
  818. if (*bh2)
  819. *bh2 = NULL;
  820. /* we always want to lock the one with the lower lockid first. */
  821. if (oi1->ip_blkno != oi2->ip_blkno) {
  822. if (oi1->ip_blkno < oi2->ip_blkno) {
  823. /* switch id1 and id2 around */
  824. mlog(0, "switching them around...\n");
  825. tmpbh = bh2;
  826. bh2 = bh1;
  827. bh1 = tmpbh;
  828. tmpinode = inode2;
  829. inode2 = inode1;
  830. inode1 = tmpinode;
  831. }
  832. /* lock id2 */
  833. status = ocfs2_inode_lock_nested(inode2, bh2, 1,
  834. OI_LS_RENAME1);
  835. if (status < 0) {
  836. if (status != -ENOENT)
  837. mlog_errno(status);
  838. goto bail;
  839. }
  840. }
  841. /* lock id1 */
  842. status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
  843. if (status < 0) {
  844. /*
  845. * An error return must mean that no cluster locks
  846. * were held on function exit.
  847. */
  848. if (oi1->ip_blkno != oi2->ip_blkno)
  849. ocfs2_inode_unlock(inode2, 1);
  850. if (status != -ENOENT)
  851. mlog_errno(status);
  852. }
  853. bail:
  854. mlog_exit(status);
  855. return status;
  856. }
  857. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  858. {
  859. ocfs2_inode_unlock(inode1, 1);
  860. if (inode1 != inode2)
  861. ocfs2_inode_unlock(inode2, 1);
  862. }
  863. static int ocfs2_rename(struct inode *old_dir,
  864. struct dentry *old_dentry,
  865. struct inode *new_dir,
  866. struct dentry *new_dentry)
  867. {
  868. int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
  869. int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
  870. struct inode *old_inode = old_dentry->d_inode;
  871. struct inode *new_inode = new_dentry->d_inode;
  872. struct inode *orphan_dir = NULL;
  873. struct ocfs2_dinode *newfe = NULL;
  874. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  875. struct buffer_head *newfe_bh = NULL;
  876. struct buffer_head *old_inode_bh = NULL;
  877. struct ocfs2_super *osb = NULL;
  878. u64 newfe_blkno, old_de_ino;
  879. handle_t *handle = NULL;
  880. struct buffer_head *old_dir_bh = NULL;
  881. struct buffer_head *new_dir_bh = NULL;
  882. nlink_t old_dir_nlink = old_dir->i_nlink;
  883. struct ocfs2_dinode *old_di;
  884. struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
  885. struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
  886. struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
  887. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  888. struct ocfs2_dir_lookup_result target_insert = { NULL, };
  889. /* At some point it might be nice to break this function up a
  890. * bit. */
  891. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  892. old_dir, old_dentry, new_dir, new_dentry,
  893. old_dentry->d_name.len, old_dentry->d_name.name,
  894. new_dentry->d_name.len, new_dentry->d_name.name);
  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, 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. status = ocfs2_journal_dirty(handle, newfe_bh);
  1131. if (status < 0) {
  1132. mlog_errno(status);
  1133. goto bail;
  1134. }
  1135. } else {
  1136. /* if the name was not found in new_dir, add it now */
  1137. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1138. OCFS2_I(old_inode)->ip_blkno,
  1139. new_dir_bh, &target_insert);
  1140. }
  1141. old_inode->i_ctime = CURRENT_TIME;
  1142. mark_inode_dirty(old_inode);
  1143. status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
  1144. old_inode_bh,
  1145. OCFS2_JOURNAL_ACCESS_WRITE);
  1146. if (status >= 0) {
  1147. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1148. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1149. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1150. status = ocfs2_journal_dirty(handle, old_inode_bh);
  1151. if (status < 0)
  1152. mlog_errno(status);
  1153. } else
  1154. mlog_errno(status);
  1155. /*
  1156. * Now that the name has been added to new_dir, remove the old name.
  1157. *
  1158. * We don't keep any directory entry context around until now
  1159. * because the insert might have changed the type of directory
  1160. * we're dealing with.
  1161. */
  1162. status = ocfs2_find_entry(old_dentry->d_name.name,
  1163. old_dentry->d_name.len, old_dir,
  1164. &old_entry_lookup);
  1165. if (status)
  1166. goto bail;
  1167. status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
  1168. if (status < 0) {
  1169. mlog_errno(status);
  1170. goto bail;
  1171. }
  1172. if (new_inode) {
  1173. new_inode->i_nlink--;
  1174. new_inode->i_ctime = CURRENT_TIME;
  1175. }
  1176. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1177. if (update_dot_dot) {
  1178. status = ocfs2_update_entry(old_inode, handle,
  1179. &old_inode_dot_dot_res, new_dir);
  1180. old_dir->i_nlink--;
  1181. if (new_inode) {
  1182. new_inode->i_nlink--;
  1183. } else {
  1184. inc_nlink(new_dir);
  1185. mark_inode_dirty(new_dir);
  1186. }
  1187. }
  1188. mark_inode_dirty(old_dir);
  1189. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1190. if (new_inode) {
  1191. mark_inode_dirty(new_inode);
  1192. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1193. }
  1194. if (old_dir != new_dir) {
  1195. /* Keep the same times on both directories.*/
  1196. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1197. /*
  1198. * This will also pick up the i_nlink change from the
  1199. * block above.
  1200. */
  1201. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1202. }
  1203. if (old_dir_nlink != old_dir->i_nlink) {
  1204. if (!old_dir_bh) {
  1205. mlog(ML_ERROR, "need to change nlink for old dir "
  1206. "%llu from %d to %d but bh is NULL!\n",
  1207. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1208. (int)old_dir_nlink, old_dir->i_nlink);
  1209. } else {
  1210. struct ocfs2_dinode *fe;
  1211. status = ocfs2_journal_access_di(handle,
  1212. INODE_CACHE(old_dir),
  1213. old_dir_bh,
  1214. OCFS2_JOURNAL_ACCESS_WRITE);
  1215. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1216. ocfs2_set_links_count(fe, old_dir->i_nlink);
  1217. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1218. }
  1219. }
  1220. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1221. status = 0;
  1222. bail:
  1223. if (rename_lock)
  1224. ocfs2_rename_unlock(osb);
  1225. if (handle)
  1226. ocfs2_commit_trans(osb, handle);
  1227. if (parents_locked)
  1228. ocfs2_double_unlock(old_dir, new_dir);
  1229. if (old_child_locked)
  1230. ocfs2_inode_unlock(old_inode, 1);
  1231. if (new_child_locked)
  1232. ocfs2_inode_unlock(new_inode, 1);
  1233. if (orphan_dir) {
  1234. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1235. ocfs2_inode_unlock(orphan_dir, 1);
  1236. mutex_unlock(&orphan_dir->i_mutex);
  1237. iput(orphan_dir);
  1238. }
  1239. if (new_inode)
  1240. sync_mapping_buffers(old_inode->i_mapping);
  1241. if (new_inode)
  1242. iput(new_inode);
  1243. ocfs2_free_dir_lookup_result(&target_lookup_res);
  1244. ocfs2_free_dir_lookup_result(&old_entry_lookup);
  1245. ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
  1246. ocfs2_free_dir_lookup_result(&orphan_insert);
  1247. ocfs2_free_dir_lookup_result(&target_insert);
  1248. brelse(newfe_bh);
  1249. brelse(old_inode_bh);
  1250. brelse(old_dir_bh);
  1251. brelse(new_dir_bh);
  1252. mlog_exit(status);
  1253. return status;
  1254. }
  1255. /*
  1256. * we expect i_size = strlen(symname). Copy symname into the file
  1257. * data, including the null terminator.
  1258. */
  1259. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1260. handle_t *handle,
  1261. struct inode *inode,
  1262. const char *symname)
  1263. {
  1264. struct buffer_head **bhs = NULL;
  1265. const char *c;
  1266. struct super_block *sb = osb->sb;
  1267. u64 p_blkno, p_blocks;
  1268. int virtual, blocks, status, i, bytes_left;
  1269. bytes_left = i_size_read(inode) + 1;
  1270. /* we can't trust i_blocks because we're actually going to
  1271. * write i_size + 1 bytes. */
  1272. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1273. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1274. (unsigned long long)inode->i_blocks,
  1275. i_size_read(inode), blocks);
  1276. /* Sanity check -- make sure we're going to fit. */
  1277. if (bytes_left >
  1278. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1279. status = -EIO;
  1280. mlog_errno(status);
  1281. goto bail;
  1282. }
  1283. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1284. if (!bhs) {
  1285. status = -ENOMEM;
  1286. mlog_errno(status);
  1287. goto bail;
  1288. }
  1289. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1290. NULL);
  1291. if (status < 0) {
  1292. mlog_errno(status);
  1293. goto bail;
  1294. }
  1295. /* links can never be larger than one cluster so we know this
  1296. * is all going to be contiguous, but do a sanity check
  1297. * anyway. */
  1298. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1299. status = -EIO;
  1300. mlog_errno(status);
  1301. goto bail;
  1302. }
  1303. virtual = 0;
  1304. while(bytes_left > 0) {
  1305. c = &symname[virtual * sb->s_blocksize];
  1306. bhs[virtual] = sb_getblk(sb, p_blkno);
  1307. if (!bhs[virtual]) {
  1308. status = -ENOMEM;
  1309. mlog_errno(status);
  1310. goto bail;
  1311. }
  1312. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
  1313. bhs[virtual]);
  1314. status = ocfs2_journal_access(handle, INODE_CACHE(inode),
  1315. bhs[virtual],
  1316. OCFS2_JOURNAL_ACCESS_CREATE);
  1317. if (status < 0) {
  1318. mlog_errno(status);
  1319. goto bail;
  1320. }
  1321. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1322. memcpy(bhs[virtual]->b_data, c,
  1323. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1324. bytes_left);
  1325. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1326. if (status < 0) {
  1327. mlog_errno(status);
  1328. goto bail;
  1329. }
  1330. virtual++;
  1331. p_blkno++;
  1332. bytes_left -= sb->s_blocksize;
  1333. }
  1334. status = 0;
  1335. bail:
  1336. if (bhs) {
  1337. for(i = 0; i < blocks; i++)
  1338. brelse(bhs[i]);
  1339. kfree(bhs);
  1340. }
  1341. mlog_exit(status);
  1342. return status;
  1343. }
  1344. static int ocfs2_symlink(struct inode *dir,
  1345. struct dentry *dentry,
  1346. const char *symname)
  1347. {
  1348. int status, l, credits;
  1349. u64 newsize;
  1350. struct ocfs2_super *osb = NULL;
  1351. struct inode *inode = NULL;
  1352. struct super_block *sb;
  1353. struct buffer_head *new_fe_bh = NULL;
  1354. struct buffer_head *parent_fe_bh = NULL;
  1355. struct ocfs2_dinode *fe = NULL;
  1356. struct ocfs2_dinode *dirfe;
  1357. handle_t *handle = NULL;
  1358. struct ocfs2_alloc_context *inode_ac = NULL;
  1359. struct ocfs2_alloc_context *data_ac = NULL;
  1360. struct ocfs2_alloc_context *xattr_ac = NULL;
  1361. int want_clusters = 0;
  1362. int xattr_credits = 0;
  1363. struct ocfs2_security_xattr_info si = {
  1364. .enable = 1,
  1365. };
  1366. int did_quota = 0, did_quota_inode = 0;
  1367. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1368. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1369. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1370. sb = dir->i_sb;
  1371. osb = OCFS2_SB(sb);
  1372. l = strlen(symname) + 1;
  1373. credits = ocfs2_calc_symlink_credits(sb);
  1374. /* lock the parent directory */
  1375. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1376. if (status < 0) {
  1377. if (status != -ENOENT)
  1378. mlog_errno(status);
  1379. return status;
  1380. }
  1381. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1382. if (!ocfs2_read_links_count(dirfe)) {
  1383. /* can't make a file in a deleted directory. */
  1384. status = -ENOENT;
  1385. goto bail;
  1386. }
  1387. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1388. dentry->d_name.len);
  1389. if (status)
  1390. goto bail;
  1391. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1392. dentry->d_name.name,
  1393. dentry->d_name.len, &lookup);
  1394. if (status < 0) {
  1395. mlog_errno(status);
  1396. goto bail;
  1397. }
  1398. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1399. if (status < 0) {
  1400. if (status != -ENOSPC)
  1401. mlog_errno(status);
  1402. goto bail;
  1403. }
  1404. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1405. if (!inode) {
  1406. status = -ENOMEM;
  1407. mlog_errno(status);
  1408. goto bail;
  1409. }
  1410. /* get security xattr */
  1411. status = ocfs2_init_security_get(inode, dir, &si);
  1412. if (status) {
  1413. if (status == -EOPNOTSUPP)
  1414. si.enable = 0;
  1415. else {
  1416. mlog_errno(status);
  1417. goto bail;
  1418. }
  1419. }
  1420. /* calculate meta data/clusters for setting security xattr */
  1421. if (si.enable) {
  1422. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1423. &xattr_credits, &xattr_ac);
  1424. if (status < 0) {
  1425. mlog_errno(status);
  1426. goto bail;
  1427. }
  1428. }
  1429. /* don't reserve bitmap space for fast symlinks. */
  1430. if (l > ocfs2_fast_symlink_chars(sb))
  1431. want_clusters += 1;
  1432. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1433. if (status < 0) {
  1434. if (status != -ENOSPC)
  1435. mlog_errno(status);
  1436. goto bail;
  1437. }
  1438. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1439. if (IS_ERR(handle)) {
  1440. status = PTR_ERR(handle);
  1441. handle = NULL;
  1442. mlog_errno(status);
  1443. goto bail;
  1444. }
  1445. /* We don't use standard VFS wrapper because we don't want vfs_dq_init
  1446. * to be called. */
  1447. if (sb_any_quota_active(osb->sb) &&
  1448. osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
  1449. status = -EDQUOT;
  1450. goto bail;
  1451. }
  1452. did_quota_inode = 1;
  1453. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry,
  1454. inode->i_mode, dentry->d_name.len,
  1455. dentry->d_name.name);
  1456. status = ocfs2_mknod_locked(osb, dir, inode,
  1457. 0, &new_fe_bh, parent_fe_bh, handle,
  1458. inode_ac);
  1459. if (status < 0) {
  1460. mlog_errno(status);
  1461. goto bail;
  1462. }
  1463. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1464. inode->i_rdev = 0;
  1465. newsize = l - 1;
  1466. if (l > ocfs2_fast_symlink_chars(sb)) {
  1467. u32 offset = 0;
  1468. inode->i_op = &ocfs2_symlink_inode_operations;
  1469. if (vfs_dq_alloc_space_nodirty(inode,
  1470. ocfs2_clusters_to_bytes(osb->sb, 1))) {
  1471. status = -EDQUOT;
  1472. goto bail;
  1473. }
  1474. did_quota = 1;
  1475. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1476. new_fe_bh,
  1477. handle, data_ac, NULL,
  1478. NULL);
  1479. if (status < 0) {
  1480. if (status != -ENOSPC && status != -EINTR) {
  1481. mlog(ML_ERROR,
  1482. "Failed to extend file to %llu\n",
  1483. (unsigned long long)newsize);
  1484. mlog_errno(status);
  1485. status = -ENOSPC;
  1486. }
  1487. goto bail;
  1488. }
  1489. i_size_write(inode, newsize);
  1490. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1491. } else {
  1492. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1493. memcpy((char *) fe->id2.i_symlink, symname, l);
  1494. i_size_write(inode, newsize);
  1495. inode->i_blocks = 0;
  1496. }
  1497. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1498. if (status < 0) {
  1499. mlog_errno(status);
  1500. goto bail;
  1501. }
  1502. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1503. status = ocfs2_create_symlink_data(osb, handle, inode,
  1504. symname);
  1505. if (status < 0) {
  1506. mlog_errno(status);
  1507. goto bail;
  1508. }
  1509. }
  1510. if (si.enable) {
  1511. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1512. xattr_ac, data_ac);
  1513. if (status < 0) {
  1514. mlog_errno(status);
  1515. goto bail;
  1516. }
  1517. }
  1518. status = ocfs2_add_entry(handle, dentry, inode,
  1519. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1520. &lookup);
  1521. if (status < 0) {
  1522. mlog_errno(status);
  1523. goto bail;
  1524. }
  1525. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1526. if (status) {
  1527. mlog_errno(status);
  1528. goto bail;
  1529. }
  1530. insert_inode_hash(inode);
  1531. dentry->d_op = &ocfs2_dentry_ops;
  1532. d_instantiate(dentry, inode);
  1533. bail:
  1534. if (status < 0 && did_quota)
  1535. vfs_dq_free_space_nodirty(inode,
  1536. ocfs2_clusters_to_bytes(osb->sb, 1));
  1537. if (status < 0 && did_quota_inode)
  1538. vfs_dq_free_inode(inode);
  1539. if (handle)
  1540. ocfs2_commit_trans(osb, handle);
  1541. ocfs2_inode_unlock(dir, 1);
  1542. brelse(new_fe_bh);
  1543. brelse(parent_fe_bh);
  1544. kfree(si.name);
  1545. kfree(si.value);
  1546. ocfs2_free_dir_lookup_result(&lookup);
  1547. if (inode_ac)
  1548. ocfs2_free_alloc_context(inode_ac);
  1549. if (data_ac)
  1550. ocfs2_free_alloc_context(data_ac);
  1551. if (xattr_ac)
  1552. ocfs2_free_alloc_context(xattr_ac);
  1553. if ((status < 0) && inode) {
  1554. clear_nlink(inode);
  1555. iput(inode);
  1556. }
  1557. mlog_exit(status);
  1558. return status;
  1559. }
  1560. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1561. {
  1562. int status, namelen;
  1563. mlog_entry_void();
  1564. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1565. (long long)blkno);
  1566. if (namelen <= 0) {
  1567. if (namelen)
  1568. status = namelen;
  1569. else
  1570. status = -EINVAL;
  1571. mlog_errno(status);
  1572. goto bail;
  1573. }
  1574. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1575. status = -EINVAL;
  1576. mlog_errno(status);
  1577. goto bail;
  1578. }
  1579. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1580. namelen);
  1581. status = 0;
  1582. bail:
  1583. mlog_exit(status);
  1584. return status;
  1585. }
  1586. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1587. struct inode **ret_orphan_dir,
  1588. u64 blkno,
  1589. char *name,
  1590. struct ocfs2_dir_lookup_result *lookup)
  1591. {
  1592. struct inode *orphan_dir_inode;
  1593. struct buffer_head *orphan_dir_bh = NULL;
  1594. int status = 0;
  1595. status = ocfs2_blkno_stringify(blkno, name);
  1596. if (status < 0) {
  1597. mlog_errno(status);
  1598. return status;
  1599. }
  1600. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1601. ORPHAN_DIR_SYSTEM_INODE,
  1602. osb->slot_num);
  1603. if (!orphan_dir_inode) {
  1604. status = -ENOENT;
  1605. mlog_errno(status);
  1606. return status;
  1607. }
  1608. mutex_lock(&orphan_dir_inode->i_mutex);
  1609. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1610. if (status < 0) {
  1611. mlog_errno(status);
  1612. goto leave;
  1613. }
  1614. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1615. orphan_dir_bh, name,
  1616. OCFS2_ORPHAN_NAMELEN, lookup);
  1617. if (status < 0) {
  1618. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1619. mlog_errno(status);
  1620. goto leave;
  1621. }
  1622. *ret_orphan_dir = orphan_dir_inode;
  1623. leave:
  1624. if (status) {
  1625. mutex_unlock(&orphan_dir_inode->i_mutex);
  1626. iput(orphan_dir_inode);
  1627. }
  1628. brelse(orphan_dir_bh);
  1629. mlog_exit(status);
  1630. return status;
  1631. }
  1632. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1633. handle_t *handle,
  1634. struct inode *inode,
  1635. struct ocfs2_dinode *fe,
  1636. char *name,
  1637. struct ocfs2_dir_lookup_result *lookup,
  1638. struct inode *orphan_dir_inode)
  1639. {
  1640. struct buffer_head *orphan_dir_bh = NULL;
  1641. int status = 0;
  1642. struct ocfs2_dinode *orphan_fe;
  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. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1664. if (status < 0) {
  1665. mlog_errno(status);
  1666. goto leave;
  1667. }
  1668. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1669. OCFS2_ORPHAN_NAMELEN, inode,
  1670. OCFS2_I(inode)->ip_blkno,
  1671. orphan_dir_bh, lookup);
  1672. if (status < 0) {
  1673. mlog_errno(status);
  1674. goto leave;
  1675. }
  1676. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1677. /* Record which orphan dir our inode now resides
  1678. * in. delete_inode will use this to determine which orphan
  1679. * dir to lock. */
  1680. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1681. mlog(0, "Inode %llu orphaned in slot %d\n",
  1682. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1683. leave:
  1684. brelse(orphan_dir_bh);
  1685. mlog_exit(status);
  1686. return status;
  1687. }
  1688. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1689. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1690. handle_t *handle,
  1691. struct inode *orphan_dir_inode,
  1692. struct inode *inode,
  1693. struct buffer_head *orphan_dir_bh)
  1694. {
  1695. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1696. struct ocfs2_dinode *orphan_fe;
  1697. int status = 0;
  1698. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1699. mlog_entry_void();
  1700. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1701. if (status < 0) {
  1702. mlog_errno(status);
  1703. goto leave;
  1704. }
  1705. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1706. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1707. OCFS2_ORPHAN_NAMELEN);
  1708. /* find it's spot in the orphan directory */
  1709. status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
  1710. &lookup);
  1711. if (status) {
  1712. mlog_errno(status);
  1713. goto leave;
  1714. }
  1715. /* remove it from the orphan directory */
  1716. status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
  1717. if (status < 0) {
  1718. mlog_errno(status);
  1719. goto leave;
  1720. }
  1721. status = ocfs2_journal_access_di(handle,
  1722. INODE_CACHE(orphan_dir_inode),
  1723. orphan_dir_bh,
  1724. OCFS2_JOURNAL_ACCESS_WRITE);
  1725. if (status < 0) {
  1726. mlog_errno(status);
  1727. goto leave;
  1728. }
  1729. /* do the i_nlink dance! :) */
  1730. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1731. if (S_ISDIR(inode->i_mode))
  1732. ocfs2_add_links_count(orphan_fe, -1);
  1733. orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
  1734. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1735. if (status < 0) {
  1736. mlog_errno(status);
  1737. goto leave;
  1738. }
  1739. leave:
  1740. ocfs2_free_dir_lookup_result(&lookup);
  1741. mlog_exit(status);
  1742. return status;
  1743. }
  1744. int ocfs2_create_inode_in_orphan(struct inode *dir,
  1745. int mode,
  1746. struct inode **new_inode)
  1747. {
  1748. int status, did_quota_inode = 0;
  1749. struct inode *inode = NULL;
  1750. struct inode *orphan_dir = NULL;
  1751. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1752. struct ocfs2_dinode *di = NULL;
  1753. handle_t *handle = NULL;
  1754. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  1755. struct buffer_head *parent_di_bh = NULL;
  1756. struct buffer_head *new_di_bh = NULL;
  1757. struct ocfs2_alloc_context *inode_ac = NULL;
  1758. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  1759. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  1760. if (status < 0) {
  1761. if (status != -ENOENT)
  1762. mlog_errno(status);
  1763. return status;
  1764. }
  1765. /*
  1766. * We give the orphan dir the root blkno to fake an orphan name,
  1767. * and allocate enough space for our insertion.
  1768. */
  1769. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1770. osb->root_blkno,
  1771. orphan_name, &orphan_insert);
  1772. if (status < 0) {
  1773. mlog_errno(status);
  1774. goto leave;
  1775. }
  1776. /* reserve an inode spot */
  1777. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1778. if (status < 0) {
  1779. if (status != -ENOSPC)
  1780. mlog_errno(status);
  1781. goto leave;
  1782. }
  1783. inode = ocfs2_get_init_inode(dir, mode);
  1784. if (!inode) {
  1785. status = -ENOMEM;
  1786. mlog_errno(status);
  1787. goto leave;
  1788. }
  1789. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
  1790. if (IS_ERR(handle)) {
  1791. status = PTR_ERR(handle);
  1792. handle = NULL;
  1793. mlog_errno(status);
  1794. goto leave;
  1795. }
  1796. /* We don't use standard VFS wrapper because we don't want vfs_dq_init
  1797. * to be called. */
  1798. if (sb_any_quota_active(osb->sb) &&
  1799. osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
  1800. status = -EDQUOT;
  1801. goto leave;
  1802. }
  1803. did_quota_inode = 1;
  1804. /* do the real work now. */
  1805. status = ocfs2_mknod_locked(osb, dir, inode,
  1806. 0, &new_di_bh, parent_di_bh, handle,
  1807. inode_ac);
  1808. if (status < 0) {
  1809. mlog_errno(status);
  1810. goto leave;
  1811. }
  1812. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, orphan_name);
  1813. if (status < 0) {
  1814. mlog_errno(status);
  1815. goto leave;
  1816. }
  1817. di = (struct ocfs2_dinode *)new_di_bh->b_data;
  1818. status = ocfs2_orphan_add(osb, handle, inode, di, orphan_name,
  1819. &orphan_insert, orphan_dir);
  1820. if (status < 0) {
  1821. mlog_errno(status);
  1822. goto leave;
  1823. }
  1824. /* get open lock so that only nodes can't remove it from orphan dir. */
  1825. status = ocfs2_open_lock(inode);
  1826. if (status < 0)
  1827. mlog_errno(status);
  1828. leave:
  1829. if (status < 0 && did_quota_inode)
  1830. vfs_dq_free_inode(inode);
  1831. if (handle)
  1832. ocfs2_commit_trans(osb, handle);
  1833. if (orphan_dir) {
  1834. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1835. ocfs2_inode_unlock(orphan_dir, 1);
  1836. mutex_unlock(&orphan_dir->i_mutex);
  1837. iput(orphan_dir);
  1838. }
  1839. if (status == -ENOSPC)
  1840. mlog(0, "Disk is full\n");
  1841. if ((status < 0) && inode) {
  1842. clear_nlink(inode);
  1843. iput(inode);
  1844. }
  1845. if (inode_ac)
  1846. ocfs2_free_alloc_context(inode_ac);
  1847. brelse(new_di_bh);
  1848. if (!status)
  1849. *new_inode = inode;
  1850. ocfs2_free_dir_lookup_result(&orphan_insert);
  1851. ocfs2_inode_unlock(dir, 1);
  1852. brelse(parent_di_bh);
  1853. return status;
  1854. }
  1855. int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
  1856. struct inode *inode,
  1857. struct dentry *dentry)
  1858. {
  1859. int status = 0;
  1860. struct buffer_head *parent_di_bh = NULL;
  1861. handle_t *handle = NULL;
  1862. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1863. struct ocfs2_dinode *dir_di, *di;
  1864. struct inode *orphan_dir_inode = NULL;
  1865. struct buffer_head *orphan_dir_bh = NULL;
  1866. struct buffer_head *di_bh = NULL;
  1867. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1868. mlog_entry("(0x%p, 0x%p, %.*s')\n", dir, dentry,
  1869. dentry->d_name.len, dentry->d_name.name);
  1870. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  1871. if (status < 0) {
  1872. if (status != -ENOENT)
  1873. mlog_errno(status);
  1874. return status;
  1875. }
  1876. dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
  1877. if (!dir_di->i_links_count) {
  1878. /* can't make a file in a deleted directory. */
  1879. status = -ENOENT;
  1880. goto leave;
  1881. }
  1882. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1883. dentry->d_name.len);
  1884. if (status)
  1885. goto leave;
  1886. /* get a spot inside the dir. */
  1887. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
  1888. dentry->d_name.name,
  1889. dentry->d_name.len, &lookup);
  1890. if (status < 0) {
  1891. mlog_errno(status);
  1892. goto leave;
  1893. }
  1894. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1895. ORPHAN_DIR_SYSTEM_INODE,
  1896. osb->slot_num);
  1897. if (!orphan_dir_inode) {
  1898. status = -EEXIST;
  1899. mlog_errno(status);
  1900. goto leave;
  1901. }
  1902. mutex_lock(&orphan_dir_inode->i_mutex);
  1903. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1904. if (status < 0) {
  1905. mlog_errno(status);
  1906. mutex_unlock(&orphan_dir_inode->i_mutex);
  1907. iput(orphan_dir_inode);
  1908. goto leave;
  1909. }
  1910. status = ocfs2_read_inode_block(inode, &di_bh);
  1911. if (status < 0) {
  1912. mlog_errno(status);
  1913. goto orphan_unlock;
  1914. }
  1915. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1916. if (IS_ERR(handle)) {
  1917. status = PTR_ERR(handle);
  1918. handle = NULL;
  1919. mlog_errno(status);
  1920. goto orphan_unlock;
  1921. }
  1922. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  1923. di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1924. if (status < 0) {
  1925. mlog_errno(status);
  1926. goto out_commit;
  1927. }
  1928. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  1929. orphan_dir_bh);
  1930. if (status < 0) {
  1931. mlog_errno(status);
  1932. goto out_commit;
  1933. }
  1934. di = (struct ocfs2_dinode *)di_bh->b_data;
  1935. le32_add_cpu(&di->i_flags, -OCFS2_ORPHANED_FL);
  1936. di->i_orphaned_slot = 0;
  1937. ocfs2_journal_dirty(handle, di_bh);
  1938. status = ocfs2_add_entry(handle, dentry, inode,
  1939. OCFS2_I(inode)->ip_blkno, parent_di_bh,
  1940. &lookup);
  1941. if (status < 0) {
  1942. mlog_errno(status);
  1943. goto out_commit;
  1944. }
  1945. status = ocfs2_dentry_attach_lock(dentry, inode,
  1946. OCFS2_I(dir)->ip_blkno);
  1947. if (status) {
  1948. mlog_errno(status);
  1949. goto out_commit;
  1950. }
  1951. insert_inode_hash(inode);
  1952. dentry->d_op = &ocfs2_dentry_ops;
  1953. d_instantiate(dentry, inode);
  1954. status = 0;
  1955. out_commit:
  1956. ocfs2_commit_trans(osb, handle);
  1957. orphan_unlock:
  1958. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1959. mutex_unlock(&orphan_dir_inode->i_mutex);
  1960. iput(orphan_dir_inode);
  1961. leave:
  1962. ocfs2_inode_unlock(dir, 1);
  1963. brelse(di_bh);
  1964. brelse(parent_di_bh);
  1965. brelse(orphan_dir_bh);
  1966. ocfs2_free_dir_lookup_result(&lookup);
  1967. mlog_exit(status);
  1968. return status;
  1969. }
  1970. const struct inode_operations ocfs2_dir_iops = {
  1971. .create = ocfs2_create,
  1972. .lookup = ocfs2_lookup,
  1973. .link = ocfs2_link,
  1974. .unlink = ocfs2_unlink,
  1975. .rmdir = ocfs2_unlink,
  1976. .symlink = ocfs2_symlink,
  1977. .mkdir = ocfs2_mkdir,
  1978. .mknod = ocfs2_mknod,
  1979. .rename = ocfs2_rename,
  1980. .setattr = ocfs2_setattr,
  1981. .getattr = ocfs2_getattr,
  1982. .permission = ocfs2_permission,
  1983. .setxattr = generic_setxattr,
  1984. .getxattr = generic_getxattr,
  1985. .listxattr = ocfs2_listxattr,
  1986. .removexattr = generic_removexattr,
  1987. };