namei.c 70 KB

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