namei.c 70 KB

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