namei.c 70 KB

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