namei.c 60 KB

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