namei.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  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. #define MLOG_MASK_PREFIX ML_NAMEI
  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 "vote.h"
  60. #include "buffer_head_io.h"
  61. #define NAMEI_RA_CHUNKS 2
  62. #define NAMEI_RA_BLOCKS 4
  63. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  64. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  65. static int inline ocfs2_search_dirblock(struct buffer_head *bh,
  66. struct inode *dir,
  67. const char *name, int namelen,
  68. unsigned long offset,
  69. struct ocfs2_dir_entry **res_dir);
  70. static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
  71. struct inode *dir,
  72. struct ocfs2_dir_entry *de_del,
  73. struct buffer_head *bh);
  74. static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
  75. struct inode *dir,
  76. const char *name, int namelen,
  77. struct inode *inode, u64 blkno,
  78. struct buffer_head *parent_fe_bh,
  79. struct buffer_head *insert_bh);
  80. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  81. struct inode *dir,
  82. struct dentry *dentry, int mode,
  83. dev_t dev,
  84. struct buffer_head **new_fe_bh,
  85. struct buffer_head *parent_fe_bh,
  86. struct ocfs2_journal_handle *handle,
  87. struct inode **ret_inode,
  88. struct ocfs2_alloc_context *inode_ac);
  89. static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  90. struct ocfs2_journal_handle *handle,
  91. struct inode *parent,
  92. struct inode *inode,
  93. struct buffer_head *fe_bh,
  94. struct ocfs2_alloc_context *data_ac);
  95. static int ocfs2_double_lock(struct ocfs2_super *osb,
  96. struct ocfs2_journal_handle *handle,
  97. struct buffer_head **bh1,
  98. struct inode *inode1,
  99. struct buffer_head **bh2,
  100. struct inode *inode2);
  101. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  102. struct inode **ret_orphan_dir,
  103. struct inode *inode,
  104. char *name,
  105. struct buffer_head **de_bh);
  106. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  107. struct ocfs2_journal_handle *handle,
  108. struct inode *inode,
  109. struct ocfs2_dinode *fe,
  110. char *name,
  111. struct buffer_head *de_bh,
  112. struct inode *orphan_dir_inode);
  113. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  114. struct ocfs2_journal_handle *handle,
  115. struct inode *inode,
  116. const char *symname);
  117. static inline int ocfs2_add_entry(struct ocfs2_journal_handle *handle,
  118. struct dentry *dentry,
  119. struct inode *inode, u64 blkno,
  120. struct buffer_head *parent_fe_bh,
  121. struct buffer_head *insert_bh)
  122. {
  123. return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
  124. dentry->d_name.name, dentry->d_name.len,
  125. inode, blkno, parent_fe_bh, insert_bh);
  126. }
  127. /* An orphan dir name is an 8 byte value, printed as a hex string */
  128. #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
  129. static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
  130. struct nameidata *nd)
  131. {
  132. int status;
  133. u64 blkno;
  134. struct buffer_head *dirent_bh = NULL;
  135. struct inode *inode = NULL;
  136. struct dentry *ret;
  137. struct ocfs2_dir_entry *dirent;
  138. struct ocfs2_inode_info *oi;
  139. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  140. dentry->d_name.len, dentry->d_name.name);
  141. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  142. ret = ERR_PTR(-ENAMETOOLONG);
  143. goto bail;
  144. }
  145. mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
  146. dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  147. status = ocfs2_meta_lock(dir, NULL, NULL, 0);
  148. if (status < 0) {
  149. if (status != -ENOENT)
  150. mlog_errno(status);
  151. ret = ERR_PTR(status);
  152. goto bail;
  153. }
  154. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  155. dentry->d_name.len, &blkno,
  156. dir, &dirent_bh, &dirent);
  157. if (status < 0)
  158. goto bail_add;
  159. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
  160. if (IS_ERR(inode)) {
  161. mlog(ML_ERROR, "Unable to create inode %llu\n",
  162. (unsigned long long)blkno);
  163. ret = ERR_PTR(-EACCES);
  164. goto bail_unlock;
  165. }
  166. oi = OCFS2_I(inode);
  167. /* Clear any orphaned state... If we were able to look up the
  168. * inode from a directory, it certainly can't be orphaned. We
  169. * might have the bad state from a node which intended to
  170. * orphan this inode but crashed before it could commit the
  171. * unlink. */
  172. spin_lock(&oi->ip_lock);
  173. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  174. oi->ip_orphaned_slot = OCFS2_INVALID_SLOT;
  175. spin_unlock(&oi->ip_lock);
  176. bail_add:
  177. dentry->d_op = &ocfs2_dentry_ops;
  178. ret = d_splice_alias(inode, dentry);
  179. if (inode) {
  180. /*
  181. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  182. * dentry, it will d_move() it on top of ourse. The
  183. * return value will indicate this however, so in
  184. * those cases, we switch them around for the locking
  185. * code.
  186. *
  187. * NOTE: This dentry already has ->d_op set from
  188. * ocfs2_get_parent() and ocfs2_get_dentry()
  189. */
  190. if (ret)
  191. dentry = ret;
  192. status = ocfs2_dentry_attach_lock(dentry, inode,
  193. OCFS2_I(dir)->ip_blkno);
  194. if (status) {
  195. mlog_errno(status);
  196. ret = ERR_PTR(status);
  197. goto bail_unlock;
  198. }
  199. }
  200. bail_unlock:
  201. /* Don't drop the cluster lock until *after* the d_add --
  202. * unlink on another node will message us to remove that
  203. * dentry under this lock so otherwise we can race this with
  204. * the vote thread and have a stale dentry. */
  205. ocfs2_meta_unlock(dir, 0);
  206. bail:
  207. if (dirent_bh)
  208. brelse(dirent_bh);
  209. mlog_exit_ptr(ret);
  210. return ret;
  211. }
  212. static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  213. struct ocfs2_journal_handle *handle,
  214. struct inode *parent,
  215. struct inode *inode,
  216. struct buffer_head *fe_bh,
  217. struct ocfs2_alloc_context *data_ac)
  218. {
  219. int status;
  220. struct buffer_head *new_bh = NULL;
  221. struct ocfs2_dir_entry *de = NULL;
  222. mlog_entry_void();
  223. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  224. data_ac, NULL, &new_bh);
  225. if (status < 0) {
  226. mlog_errno(status);
  227. goto bail;
  228. }
  229. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  230. status = ocfs2_journal_access(handle, inode, new_bh,
  231. OCFS2_JOURNAL_ACCESS_CREATE);
  232. if (status < 0) {
  233. mlog_errno(status);
  234. goto bail;
  235. }
  236. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  237. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  238. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  239. de->name_len = 1;
  240. de->rec_len =
  241. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  242. strcpy(de->name, ".");
  243. ocfs2_set_de_type(de, S_IFDIR);
  244. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  245. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  246. de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
  247. OCFS2_DIR_REC_LEN(1));
  248. de->name_len = 2;
  249. strcpy(de->name, "..");
  250. ocfs2_set_de_type(de, S_IFDIR);
  251. status = ocfs2_journal_dirty(handle, new_bh);
  252. if (status < 0) {
  253. mlog_errno(status);
  254. goto bail;
  255. }
  256. i_size_write(inode, inode->i_sb->s_blocksize);
  257. inode->i_nlink = 2;
  258. inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
  259. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  260. if (status < 0) {
  261. mlog_errno(status);
  262. goto bail;
  263. }
  264. status = 0;
  265. bail:
  266. if (new_bh)
  267. brelse(new_bh);
  268. mlog_exit(status);
  269. return status;
  270. }
  271. static int ocfs2_mknod(struct inode *dir,
  272. struct dentry *dentry,
  273. int mode,
  274. dev_t dev)
  275. {
  276. int status = 0;
  277. struct buffer_head *parent_fe_bh = NULL;
  278. struct ocfs2_journal_handle *handle = NULL;
  279. struct ocfs2_super *osb;
  280. struct ocfs2_dinode *dirfe;
  281. struct buffer_head *new_fe_bh = NULL;
  282. struct buffer_head *de_bh = NULL;
  283. struct inode *inode = NULL;
  284. struct ocfs2_alloc_context *inode_ac = NULL;
  285. struct ocfs2_alloc_context *data_ac = NULL;
  286. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  287. (unsigned long)dev, dentry->d_name.len,
  288. dentry->d_name.name);
  289. /* get our super block */
  290. osb = OCFS2_SB(dir->i_sb);
  291. status = ocfs2_meta_lock(dir, NULL, &parent_fe_bh, 1);
  292. if (status < 0) {
  293. if (status != -ENOENT)
  294. mlog_errno(status);
  295. return status;
  296. }
  297. handle = ocfs2_alloc_handle(osb);
  298. if (handle == NULL) {
  299. status = -ENOMEM;
  300. mlog_errno(status);
  301. goto leave;
  302. }
  303. if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
  304. status = -EMLINK;
  305. goto leave;
  306. }
  307. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  308. if (!dirfe->i_links_count) {
  309. /* can't make a file in a deleted directory. */
  310. status = -ENOENT;
  311. goto leave;
  312. }
  313. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  314. dentry->d_name.len);
  315. if (status)
  316. goto leave;
  317. /* get a spot inside the dir. */
  318. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  319. dentry->d_name.name,
  320. dentry->d_name.len, &de_bh);
  321. if (status < 0) {
  322. mlog_errno(status);
  323. goto leave;
  324. }
  325. /* reserve an inode spot */
  326. status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
  327. if (status < 0) {
  328. if (status != -ENOSPC)
  329. mlog_errno(status);
  330. goto leave;
  331. }
  332. /* are we making a directory? If so, reserve a cluster for his
  333. * 1st extent. */
  334. if (S_ISDIR(mode)) {
  335. status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
  336. if (status < 0) {
  337. if (status != -ENOSPC)
  338. mlog_errno(status);
  339. goto leave;
  340. }
  341. }
  342. handle = ocfs2_start_trans(osb, handle, OCFS2_MKNOD_CREDITS);
  343. if (IS_ERR(handle)) {
  344. status = PTR_ERR(handle);
  345. handle = NULL;
  346. mlog_errno(status);
  347. goto leave;
  348. }
  349. /* do the real work now. */
  350. status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
  351. &new_fe_bh, parent_fe_bh, handle,
  352. &inode, inode_ac);
  353. if (status < 0) {
  354. mlog_errno(status);
  355. goto leave;
  356. }
  357. if (S_ISDIR(mode)) {
  358. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  359. new_fe_bh, data_ac);
  360. if (status < 0) {
  361. mlog_errno(status);
  362. goto leave;
  363. }
  364. status = ocfs2_journal_access(handle, dir, parent_fe_bh,
  365. OCFS2_JOURNAL_ACCESS_WRITE);
  366. if (status < 0) {
  367. mlog_errno(status);
  368. goto leave;
  369. }
  370. le16_add_cpu(&dirfe->i_links_count, 1);
  371. status = ocfs2_journal_dirty(handle, parent_fe_bh);
  372. if (status < 0) {
  373. mlog_errno(status);
  374. goto leave;
  375. }
  376. inc_nlink(dir);
  377. }
  378. status = ocfs2_add_entry(handle, dentry, inode,
  379. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  380. de_bh);
  381. if (status < 0) {
  382. mlog_errno(status);
  383. goto leave;
  384. }
  385. status = ocfs2_dentry_attach_lock(dentry, inode,
  386. OCFS2_I(dir)->ip_blkno);
  387. if (status) {
  388. mlog_errno(status);
  389. goto leave;
  390. }
  391. insert_inode_hash(inode);
  392. dentry->d_op = &ocfs2_dentry_ops;
  393. d_instantiate(dentry, inode);
  394. status = 0;
  395. leave:
  396. if (handle)
  397. ocfs2_commit_trans(handle);
  398. ocfs2_meta_unlock(dir, 1);
  399. if (status == -ENOSPC)
  400. mlog(0, "Disk is full\n");
  401. if (new_fe_bh)
  402. brelse(new_fe_bh);
  403. if (de_bh)
  404. brelse(de_bh);
  405. if (parent_fe_bh)
  406. brelse(parent_fe_bh);
  407. if ((status < 0) && inode)
  408. iput(inode);
  409. if (inode_ac)
  410. ocfs2_free_alloc_context(inode_ac);
  411. if (data_ac)
  412. ocfs2_free_alloc_context(data_ac);
  413. mlog_exit(status);
  414. return status;
  415. }
  416. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  417. struct inode *dir,
  418. struct dentry *dentry, int mode,
  419. dev_t dev,
  420. struct buffer_head **new_fe_bh,
  421. struct buffer_head *parent_fe_bh,
  422. struct ocfs2_journal_handle *handle,
  423. struct inode **ret_inode,
  424. struct ocfs2_alloc_context *inode_ac)
  425. {
  426. int status = 0;
  427. struct ocfs2_dinode *fe = NULL;
  428. struct ocfs2_extent_list *fel;
  429. u64 fe_blkno = 0;
  430. u16 suballoc_bit;
  431. struct inode *inode = NULL;
  432. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  433. (unsigned long)dev, dentry->d_name.len,
  434. dentry->d_name.name);
  435. *new_fe_bh = NULL;
  436. *ret_inode = NULL;
  437. status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
  438. &fe_blkno);
  439. if (status < 0) {
  440. mlog_errno(status);
  441. goto leave;
  442. }
  443. inode = new_inode(dir->i_sb);
  444. if (IS_ERR(inode)) {
  445. status = PTR_ERR(inode);
  446. mlog(ML_ERROR, "new_inode failed!\n");
  447. goto leave;
  448. }
  449. /* populate as many fields early on as possible - many of
  450. * these are used by the support functions here and in
  451. * callers. */
  452. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  453. OCFS2_I(inode)->ip_blkno = fe_blkno;
  454. if (S_ISDIR(mode))
  455. inode->i_nlink = 2;
  456. else
  457. inode->i_nlink = 1;
  458. inode->i_mode = mode;
  459. spin_lock(&osb->osb_lock);
  460. inode->i_generation = osb->s_next_generation++;
  461. spin_unlock(&osb->osb_lock);
  462. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  463. if (!*new_fe_bh) {
  464. status = -EIO;
  465. mlog_errno(status);
  466. goto leave;
  467. }
  468. ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
  469. status = ocfs2_journal_access(handle, inode, *new_fe_bh,
  470. OCFS2_JOURNAL_ACCESS_CREATE);
  471. if (status < 0) {
  472. mlog_errno(status);
  473. goto leave;
  474. }
  475. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  476. memset(fe, 0, osb->sb->s_blocksize);
  477. fe->i_generation = cpu_to_le32(inode->i_generation);
  478. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  479. fe->i_blkno = cpu_to_le64(fe_blkno);
  480. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  481. fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
  482. fe->i_uid = cpu_to_le32(current->fsuid);
  483. if (dir->i_mode & S_ISGID) {
  484. fe->i_gid = cpu_to_le32(dir->i_gid);
  485. if (S_ISDIR(mode))
  486. mode |= S_ISGID;
  487. } else
  488. fe->i_gid = cpu_to_le32(current->fsgid);
  489. fe->i_mode = cpu_to_le16(mode);
  490. if (S_ISCHR(mode) || S_ISBLK(mode))
  491. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  492. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  493. fe->i_last_eb_blk = 0;
  494. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  495. le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
  496. fe->i_atime = fe->i_ctime = fe->i_mtime =
  497. cpu_to_le64(CURRENT_TIME.tv_sec);
  498. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  499. cpu_to_le32(CURRENT_TIME.tv_nsec);
  500. fe->i_dtime = 0;
  501. fel = &fe->id2.i_list;
  502. fel->l_tree_depth = 0;
  503. fel->l_next_free_rec = 0;
  504. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  505. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  506. if (status < 0) {
  507. mlog_errno(status);
  508. goto leave;
  509. }
  510. if (ocfs2_populate_inode(inode, fe, 1) < 0) {
  511. mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
  512. "i_blkno=%llu, i_ino=%lu\n",
  513. (unsigned long long) (*new_fe_bh)->b_blocknr,
  514. (unsigned long long)fe->i_blkno, inode->i_ino);
  515. BUG();
  516. }
  517. ocfs2_inode_set_new(osb, inode);
  518. status = ocfs2_create_new_inode_locks(inode);
  519. if (status < 0)
  520. mlog_errno(status);
  521. status = 0; /* error in ocfs2_create_new_inode_locks is not
  522. * critical */
  523. *ret_inode = inode;
  524. leave:
  525. if (status < 0) {
  526. if (*new_fe_bh) {
  527. brelse(*new_fe_bh);
  528. *new_fe_bh = NULL;
  529. }
  530. if (inode)
  531. iput(inode);
  532. }
  533. mlog_exit(status);
  534. return status;
  535. }
  536. static int ocfs2_mkdir(struct inode *dir,
  537. struct dentry *dentry,
  538. int mode)
  539. {
  540. int ret;
  541. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  542. dentry->d_name.len, dentry->d_name.name);
  543. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  544. mlog_exit(ret);
  545. return ret;
  546. }
  547. static int ocfs2_create(struct inode *dir,
  548. struct dentry *dentry,
  549. int mode,
  550. struct nameidata *nd)
  551. {
  552. int ret;
  553. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  554. dentry->d_name.len, dentry->d_name.name);
  555. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  556. mlog_exit(ret);
  557. return ret;
  558. }
  559. static int ocfs2_link(struct dentry *old_dentry,
  560. struct inode *dir,
  561. struct dentry *dentry)
  562. {
  563. struct ocfs2_journal_handle *handle;
  564. struct inode *inode = old_dentry->d_inode;
  565. int err;
  566. struct buffer_head *fe_bh = NULL;
  567. struct buffer_head *parent_fe_bh = NULL;
  568. struct buffer_head *de_bh = NULL;
  569. struct ocfs2_dinode *fe = NULL;
  570. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  571. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  572. old_dentry->d_name.len, old_dentry->d_name.name,
  573. dentry->d_name.len, dentry->d_name.name);
  574. if (S_ISDIR(inode->i_mode))
  575. return -EPERM;
  576. err = ocfs2_meta_lock(dir, NULL, &parent_fe_bh, 1);
  577. if (err < 0) {
  578. if (err != -ENOENT)
  579. mlog_errno(err);
  580. return err;
  581. }
  582. if (!dir->i_nlink) {
  583. err = -ENOENT;
  584. goto out;
  585. }
  586. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  587. dentry->d_name.len);
  588. if (err)
  589. goto out;
  590. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  591. dentry->d_name.name,
  592. dentry->d_name.len, &de_bh);
  593. if (err < 0) {
  594. mlog_errno(err);
  595. goto out;
  596. }
  597. err = ocfs2_meta_lock(inode, NULL, &fe_bh, 1);
  598. if (err < 0) {
  599. if (err != -ENOENT)
  600. mlog_errno(err);
  601. goto out;
  602. }
  603. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  604. if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
  605. err = -EMLINK;
  606. goto out_unlock_inode;
  607. }
  608. handle = ocfs2_start_trans(osb, NULL, OCFS2_LINK_CREDITS);
  609. if (IS_ERR(handle)) {
  610. err = PTR_ERR(handle);
  611. handle = NULL;
  612. mlog_errno(err);
  613. goto out_unlock_inode;
  614. }
  615. err = ocfs2_journal_access(handle, inode, fe_bh,
  616. OCFS2_JOURNAL_ACCESS_WRITE);
  617. if (err < 0) {
  618. mlog_errno(err);
  619. goto out_commit;
  620. }
  621. inc_nlink(inode);
  622. inode->i_ctime = CURRENT_TIME;
  623. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  624. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  625. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  626. err = ocfs2_journal_dirty(handle, fe_bh);
  627. if (err < 0) {
  628. le16_add_cpu(&fe->i_links_count, -1);
  629. drop_nlink(inode);
  630. mlog_errno(err);
  631. goto out_commit;
  632. }
  633. err = ocfs2_add_entry(handle, dentry, inode,
  634. OCFS2_I(inode)->ip_blkno,
  635. parent_fe_bh, de_bh);
  636. if (err) {
  637. le16_add_cpu(&fe->i_links_count, -1);
  638. drop_nlink(inode);
  639. mlog_errno(err);
  640. goto out_commit;
  641. }
  642. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  643. if (err) {
  644. mlog_errno(err);
  645. goto out_commit;
  646. }
  647. atomic_inc(&inode->i_count);
  648. dentry->d_op = &ocfs2_dentry_ops;
  649. d_instantiate(dentry, inode);
  650. out_commit:
  651. ocfs2_commit_trans(handle);
  652. out_unlock_inode:
  653. ocfs2_meta_unlock(inode, 1);
  654. out:
  655. ocfs2_meta_unlock(dir, 1);
  656. if (de_bh)
  657. brelse(de_bh);
  658. if (fe_bh)
  659. brelse(fe_bh);
  660. if (parent_fe_bh)
  661. brelse(parent_fe_bh);
  662. mlog_exit(err);
  663. return err;
  664. }
  665. /*
  666. * Takes and drops an exclusive lock on the given dentry. This will
  667. * force other nodes to drop it.
  668. */
  669. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  670. {
  671. int ret;
  672. ret = ocfs2_dentry_lock(dentry, 1);
  673. if (ret)
  674. mlog_errno(ret);
  675. else
  676. ocfs2_dentry_unlock(dentry, 1);
  677. return ret;
  678. }
  679. static inline int inode_is_unlinkable(struct inode *inode)
  680. {
  681. if (S_ISDIR(inode->i_mode)) {
  682. if (inode->i_nlink == 2)
  683. return 1;
  684. return 0;
  685. }
  686. if (inode->i_nlink == 1)
  687. return 1;
  688. return 0;
  689. }
  690. static int ocfs2_unlink(struct inode *dir,
  691. struct dentry *dentry)
  692. {
  693. int status;
  694. int child_locked = 0;
  695. struct inode *inode = dentry->d_inode;
  696. struct inode *orphan_dir = NULL;
  697. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  698. u64 blkno;
  699. struct ocfs2_dinode *fe = NULL;
  700. struct buffer_head *fe_bh = NULL;
  701. struct buffer_head *parent_node_bh = NULL;
  702. struct ocfs2_journal_handle *handle = NULL;
  703. struct ocfs2_dir_entry *dirent = NULL;
  704. struct buffer_head *dirent_bh = NULL;
  705. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  706. struct buffer_head *orphan_entry_bh = NULL;
  707. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  708. dentry->d_name.len, dentry->d_name.name);
  709. BUG_ON(dentry->d_parent->d_inode != dir);
  710. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  711. if (inode == osb->root_inode) {
  712. mlog(0, "Cannot delete the root directory\n");
  713. return -EPERM;
  714. }
  715. status = ocfs2_meta_lock(dir, NULL, &parent_node_bh, 1);
  716. if (status < 0) {
  717. if (status != -ENOENT)
  718. mlog_errno(status);
  719. return status;
  720. }
  721. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  722. dentry->d_name.len, &blkno,
  723. dir, &dirent_bh, &dirent);
  724. if (status < 0) {
  725. if (status != -ENOENT)
  726. mlog_errno(status);
  727. goto leave;
  728. }
  729. if (OCFS2_I(inode)->ip_blkno != blkno) {
  730. status = -ENOENT;
  731. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  732. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  733. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  734. goto leave;
  735. }
  736. status = ocfs2_meta_lock(inode, NULL, &fe_bh, 1);
  737. if (status < 0) {
  738. if (status != -ENOENT)
  739. mlog_errno(status);
  740. goto leave;
  741. }
  742. child_locked = 1;
  743. if (S_ISDIR(inode->i_mode)) {
  744. if (!ocfs2_empty_dir(inode)) {
  745. status = -ENOTEMPTY;
  746. goto leave;
  747. } else if (inode->i_nlink != 2) {
  748. status = -ENOTEMPTY;
  749. goto leave;
  750. }
  751. }
  752. status = ocfs2_remote_dentry_delete(dentry);
  753. if (status < 0) {
  754. /* This vote should succeed under all normal
  755. * circumstances. */
  756. mlog_errno(status);
  757. goto leave;
  758. }
  759. if (inode_is_unlinkable(inode)) {
  760. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
  761. orphan_name,
  762. &orphan_entry_bh);
  763. if (status < 0) {
  764. mlog_errno(status);
  765. goto leave;
  766. }
  767. }
  768. handle = ocfs2_start_trans(osb, NULL, OCFS2_UNLINK_CREDITS);
  769. if (IS_ERR(handle)) {
  770. status = PTR_ERR(handle);
  771. handle = NULL;
  772. mlog_errno(status);
  773. goto leave;
  774. }
  775. status = ocfs2_journal_access(handle, inode, fe_bh,
  776. OCFS2_JOURNAL_ACCESS_WRITE);
  777. if (status < 0) {
  778. mlog_errno(status);
  779. goto leave;
  780. }
  781. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  782. if (inode_is_unlinkable(inode)) {
  783. status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
  784. orphan_entry_bh, orphan_dir);
  785. if (status < 0) {
  786. mlog_errno(status);
  787. goto leave;
  788. }
  789. }
  790. /* delete the name from the parent dir */
  791. status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
  792. if (status < 0) {
  793. mlog_errno(status);
  794. goto leave;
  795. }
  796. if (S_ISDIR(inode->i_mode))
  797. drop_nlink(inode);
  798. drop_nlink(inode);
  799. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  800. status = ocfs2_journal_dirty(handle, fe_bh);
  801. if (status < 0) {
  802. mlog_errno(status);
  803. goto leave;
  804. }
  805. if (S_ISDIR(inode->i_mode)) {
  806. drop_nlink(dir);
  807. status = ocfs2_mark_inode_dirty(handle, dir,
  808. parent_node_bh);
  809. if (status < 0) {
  810. mlog_errno(status);
  811. inc_nlink(dir);
  812. }
  813. }
  814. leave:
  815. if (handle)
  816. ocfs2_commit_trans(handle);
  817. if (child_locked)
  818. ocfs2_meta_unlock(inode, 1);
  819. ocfs2_meta_unlock(dir, 1);
  820. if (orphan_dir) {
  821. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  822. ocfs2_meta_unlock(orphan_dir, 1);
  823. mutex_unlock(&orphan_dir->i_mutex);
  824. iput(orphan_dir);
  825. }
  826. if (fe_bh)
  827. brelse(fe_bh);
  828. if (dirent_bh)
  829. brelse(dirent_bh);
  830. if (parent_node_bh)
  831. brelse(parent_node_bh);
  832. if (orphan_entry_bh)
  833. brelse(orphan_entry_bh);
  834. mlog_exit(status);
  835. return status;
  836. }
  837. /*
  838. * The only place this should be used is rename!
  839. * if they have the same id, then the 1st one is the only one locked.
  840. */
  841. static int ocfs2_double_lock(struct ocfs2_super *osb,
  842. struct ocfs2_journal_handle *handle,
  843. struct buffer_head **bh1,
  844. struct inode *inode1,
  845. struct buffer_head **bh2,
  846. struct inode *inode2)
  847. {
  848. int status;
  849. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  850. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  851. struct buffer_head **tmpbh;
  852. struct inode *tmpinode;
  853. mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
  854. (unsigned long long)oi1->ip_blkno,
  855. (unsigned long long)oi2->ip_blkno);
  856. BUG_ON(!handle);
  857. if (*bh1)
  858. *bh1 = NULL;
  859. if (*bh2)
  860. *bh2 = NULL;
  861. /* we always want to lock the one with the lower lockid first. */
  862. if (oi1->ip_blkno != oi2->ip_blkno) {
  863. if (oi1->ip_blkno < oi2->ip_blkno) {
  864. /* switch id1 and id2 around */
  865. mlog(0, "switching them around...\n");
  866. tmpbh = bh2;
  867. bh2 = bh1;
  868. bh1 = tmpbh;
  869. tmpinode = inode2;
  870. inode2 = inode1;
  871. inode1 = tmpinode;
  872. }
  873. /* lock id2 */
  874. status = ocfs2_meta_lock(inode2, handle, bh2, 1);
  875. if (status < 0) {
  876. if (status != -ENOENT)
  877. mlog_errno(status);
  878. goto bail;
  879. }
  880. }
  881. /* lock id1 */
  882. status = ocfs2_meta_lock(inode1, handle, bh1, 1);
  883. if (status < 0) {
  884. if (status != -ENOENT)
  885. mlog_errno(status);
  886. goto bail;
  887. }
  888. bail:
  889. mlog_exit(status);
  890. return status;
  891. }
  892. #define PARENT_INO(buffer) \
  893. ((struct ocfs2_dir_entry *) \
  894. ((char *)buffer + \
  895. le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
  896. static int ocfs2_rename(struct inode *old_dir,
  897. struct dentry *old_dentry,
  898. struct inode *new_dir,
  899. struct dentry *new_dentry)
  900. {
  901. int status = 0, rename_lock = 0;
  902. struct inode *old_inode = old_dentry->d_inode;
  903. struct inode *new_inode = new_dentry->d_inode;
  904. struct inode *orphan_dir = NULL;
  905. struct ocfs2_dinode *newfe = NULL;
  906. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  907. struct buffer_head *orphan_entry_bh = NULL;
  908. struct buffer_head *newfe_bh = NULL;
  909. struct buffer_head *insert_entry_bh = NULL;
  910. struct ocfs2_super *osb = NULL;
  911. u64 newfe_blkno;
  912. struct ocfs2_journal_handle *handle = NULL;
  913. struct buffer_head *old_dir_bh = NULL;
  914. struct buffer_head *new_dir_bh = NULL;
  915. struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
  916. // and new_dentry
  917. struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
  918. struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
  919. // this is the 1st dirent bh
  920. nlink_t old_dir_nlink = old_dir->i_nlink, new_dir_nlink = new_dir->i_nlink;
  921. /* At some point it might be nice to break this function up a
  922. * bit. */
  923. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  924. old_dir, old_dentry, new_dir, new_dentry,
  925. old_dentry->d_name.len, old_dentry->d_name.name,
  926. new_dentry->d_name.len, new_dentry->d_name.name);
  927. osb = OCFS2_SB(old_dir->i_sb);
  928. if (new_inode) {
  929. if (!igrab(new_inode))
  930. BUG();
  931. }
  932. /* Assume a directory heirarchy thusly:
  933. * a/b/c
  934. * a/d
  935. * a,b,c, and d are all directories.
  936. *
  937. * from cwd of 'a' on both nodes:
  938. * node1: mv b/c d
  939. * node2: mv d b/c
  940. *
  941. * And that's why, just like the VFS, we need a file system
  942. * rename lock. */
  943. if (old_dentry != new_dentry) {
  944. status = ocfs2_rename_lock(osb);
  945. if (status < 0) {
  946. mlog_errno(status);
  947. goto bail;
  948. }
  949. rename_lock = 1;
  950. }
  951. handle = ocfs2_alloc_handle(osb);
  952. if (handle == NULL) {
  953. status = -ENOMEM;
  954. mlog_errno(status);
  955. goto bail;
  956. }
  957. /* if old and new are the same, this'll just do one lock. */
  958. status = ocfs2_double_lock(osb, handle,
  959. &old_dir_bh, old_dir,
  960. &new_dir_bh, new_dir);
  961. if (status < 0) {
  962. mlog_errno(status);
  963. goto bail;
  964. }
  965. /* make sure both dirs have bhs
  966. * get an extra ref on old_dir_bh if old==new */
  967. if (!new_dir_bh) {
  968. if (old_dir_bh) {
  969. new_dir_bh = old_dir_bh;
  970. get_bh(new_dir_bh);
  971. } else {
  972. mlog(ML_ERROR, "no old_dir_bh!\n");
  973. status = -EIO;
  974. goto bail;
  975. }
  976. }
  977. /*
  978. * Though we don't require an inode meta data update if
  979. * old_inode is not a directory, we lock anyway here to ensure
  980. * the vote thread on other nodes won't have to concurrently
  981. * downconvert the inode and the dentry locks.
  982. */
  983. status = ocfs2_meta_lock(old_inode, handle, NULL, 1);
  984. if (status < 0) {
  985. if (status != -ENOENT)
  986. mlog_errno(status);
  987. goto bail;
  988. }
  989. status = ocfs2_remote_dentry_delete(old_dentry);
  990. if (status < 0) {
  991. mlog_errno(status);
  992. goto bail;
  993. }
  994. if (S_ISDIR(old_inode->i_mode)) {
  995. status = -EIO;
  996. old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
  997. if (!old_inode_de_bh)
  998. goto bail;
  999. status = -EIO;
  1000. if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
  1001. OCFS2_I(old_dir)->ip_blkno)
  1002. goto bail;
  1003. status = -EMLINK;
  1004. if (!new_inode && new_dir!=old_dir &&
  1005. new_dir->i_nlink >= OCFS2_LINK_MAX)
  1006. goto bail;
  1007. }
  1008. status = -ENOENT;
  1009. old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
  1010. old_dentry->d_name.len,
  1011. old_dir, &old_de);
  1012. if (!old_de_bh)
  1013. goto bail;
  1014. /*
  1015. * Check for inode number is _not_ due to possible IO errors.
  1016. * We might rmdir the source, keep it as pwd of some process
  1017. * and merrily kill the link to whatever was created under the
  1018. * same name. Goodbye sticky bit ;-<
  1019. */
  1020. if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
  1021. goto bail;
  1022. /* check if the target already exists (in which case we need
  1023. * to delete it */
  1024. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  1025. new_dentry->d_name.len,
  1026. &newfe_blkno, new_dir, &new_de_bh,
  1027. &new_de);
  1028. /* The only error we allow here is -ENOENT because the new
  1029. * file not existing is perfectly valid. */
  1030. if ((status < 0) && (status != -ENOENT)) {
  1031. /* If we cannot find the file specified we should just */
  1032. /* return the error... */
  1033. mlog_errno(status);
  1034. goto bail;
  1035. }
  1036. if (!new_de && new_inode)
  1037. mlog(ML_ERROR, "inode %lu does not exist in it's parent "
  1038. "directory!", new_inode->i_ino);
  1039. /* In case we need to overwrite an existing file, we blow it
  1040. * away first */
  1041. if (new_de) {
  1042. /* VFS didn't think there existed an inode here, but
  1043. * someone else in the cluster must have raced our
  1044. * rename to create one. Today we error cleanly, in
  1045. * the future we should consider calling iget to build
  1046. * a new struct inode for this entry. */
  1047. if (!new_inode) {
  1048. status = -EACCES;
  1049. mlog(0, "We found an inode for name %.*s but VFS "
  1050. "didn't give us one.\n", new_dentry->d_name.len,
  1051. new_dentry->d_name.name);
  1052. goto bail;
  1053. }
  1054. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1055. status = -EACCES;
  1056. mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
  1057. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1058. (unsigned long long)newfe_blkno,
  1059. OCFS2_I(new_inode)->ip_flags);
  1060. goto bail;
  1061. }
  1062. status = ocfs2_meta_lock(new_inode, handle, &newfe_bh, 1);
  1063. if (status < 0) {
  1064. if (status != -ENOENT)
  1065. mlog_errno(status);
  1066. goto bail;
  1067. }
  1068. status = ocfs2_remote_dentry_delete(new_dentry);
  1069. if (status < 0) {
  1070. mlog_errno(status);
  1071. goto bail;
  1072. }
  1073. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1074. mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
  1075. "newfebh=%p bhblocknr=%llu\n", new_de,
  1076. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1077. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1078. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1079. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1080. new_inode,
  1081. orphan_name,
  1082. &orphan_entry_bh);
  1083. if (status < 0) {
  1084. mlog_errno(status);
  1085. goto bail;
  1086. }
  1087. }
  1088. } else {
  1089. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  1090. status = ocfs2_check_dir_for_entry(new_dir,
  1091. new_dentry->d_name.name,
  1092. new_dentry->d_name.len);
  1093. if (status)
  1094. goto bail;
  1095. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1096. new_dentry->d_name.name,
  1097. new_dentry->d_name.len,
  1098. &insert_entry_bh);
  1099. if (status < 0) {
  1100. mlog_errno(status);
  1101. goto bail;
  1102. }
  1103. }
  1104. handle = ocfs2_start_trans(osb, handle, OCFS2_RENAME_CREDITS);
  1105. if (IS_ERR(handle)) {
  1106. status = PTR_ERR(handle);
  1107. handle = NULL;
  1108. mlog_errno(status);
  1109. goto bail;
  1110. }
  1111. if (new_de) {
  1112. if (S_ISDIR(new_inode->i_mode)) {
  1113. if (!ocfs2_empty_dir(new_inode) ||
  1114. new_inode->i_nlink != 2) {
  1115. status = -ENOTEMPTY;
  1116. goto bail;
  1117. }
  1118. }
  1119. status = ocfs2_journal_access(handle, new_inode, newfe_bh,
  1120. OCFS2_JOURNAL_ACCESS_WRITE);
  1121. if (status < 0) {
  1122. mlog_errno(status);
  1123. goto bail;
  1124. }
  1125. if (S_ISDIR(new_inode->i_mode) ||
  1126. (newfe->i_links_count == cpu_to_le16(1))){
  1127. status = ocfs2_orphan_add(osb, handle, new_inode,
  1128. newfe, orphan_name,
  1129. orphan_entry_bh, orphan_dir);
  1130. if (status < 0) {
  1131. mlog_errno(status);
  1132. goto bail;
  1133. }
  1134. }
  1135. /* change the dirent to point to the correct inode */
  1136. status = ocfs2_journal_access(handle, new_dir, new_de_bh,
  1137. OCFS2_JOURNAL_ACCESS_WRITE);
  1138. if (status < 0) {
  1139. mlog_errno(status);
  1140. goto bail;
  1141. }
  1142. new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
  1143. new_de->file_type = old_de->file_type;
  1144. new_dir->i_version++;
  1145. status = ocfs2_journal_dirty(handle, new_de_bh);
  1146. if (status < 0) {
  1147. mlog_errno(status);
  1148. goto bail;
  1149. }
  1150. if (S_ISDIR(new_inode->i_mode))
  1151. newfe->i_links_count = 0;
  1152. else
  1153. le16_add_cpu(&newfe->i_links_count, -1);
  1154. status = ocfs2_journal_dirty(handle, newfe_bh);
  1155. if (status < 0) {
  1156. mlog_errno(status);
  1157. goto bail;
  1158. }
  1159. } else {
  1160. /* if the name was not found in new_dir, add it now */
  1161. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1162. OCFS2_I(old_inode)->ip_blkno,
  1163. new_dir_bh, insert_entry_bh);
  1164. }
  1165. old_inode->i_ctime = CURRENT_TIME;
  1166. mark_inode_dirty(old_inode);
  1167. /* now that the name has been added to new_dir, remove the old name */
  1168. status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
  1169. if (status < 0) {
  1170. mlog_errno(status);
  1171. goto bail;
  1172. }
  1173. if (new_inode) {
  1174. new_inode->i_nlink--;
  1175. new_inode->i_ctime = CURRENT_TIME;
  1176. }
  1177. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1178. if (old_inode_de_bh) {
  1179. status = ocfs2_journal_access(handle, old_inode,
  1180. old_inode_de_bh,
  1181. OCFS2_JOURNAL_ACCESS_WRITE);
  1182. PARENT_INO(old_inode_de_bh->b_data) =
  1183. cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
  1184. status = ocfs2_journal_dirty(handle, old_inode_de_bh);
  1185. old_dir->i_nlink--;
  1186. if (new_inode) {
  1187. new_inode->i_nlink--;
  1188. } else {
  1189. inc_nlink(new_dir);
  1190. mark_inode_dirty(new_dir);
  1191. }
  1192. }
  1193. mark_inode_dirty(old_dir);
  1194. if (new_inode)
  1195. mark_inode_dirty(new_inode);
  1196. if (old_dir != new_dir)
  1197. if (new_dir_nlink != new_dir->i_nlink) {
  1198. if (!new_dir_bh) {
  1199. mlog(ML_ERROR, "need to change nlink for new "
  1200. "dir %llu from %d to %d but bh is NULL\n",
  1201. (unsigned long long)OCFS2_I(new_dir)->ip_blkno,
  1202. (int)new_dir_nlink, new_dir->i_nlink);
  1203. } else {
  1204. struct ocfs2_dinode *fe;
  1205. status = ocfs2_journal_access(handle,
  1206. new_dir,
  1207. new_dir_bh,
  1208. OCFS2_JOURNAL_ACCESS_WRITE);
  1209. fe = (struct ocfs2_dinode *) new_dir_bh->b_data;
  1210. fe->i_links_count = cpu_to_le16(new_dir->i_nlink);
  1211. status = ocfs2_journal_dirty(handle, new_dir_bh);
  1212. }
  1213. }
  1214. if (old_dir_nlink != old_dir->i_nlink) {
  1215. if (!old_dir_bh) {
  1216. mlog(ML_ERROR, "need to change nlink for old dir "
  1217. "%llu from %d to %d but bh is NULL!\n",
  1218. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1219. (int)old_dir_nlink, old_dir->i_nlink);
  1220. } else {
  1221. struct ocfs2_dinode *fe;
  1222. status = ocfs2_journal_access(handle, old_dir,
  1223. old_dir_bh,
  1224. OCFS2_JOURNAL_ACCESS_WRITE);
  1225. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1226. fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
  1227. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1228. }
  1229. }
  1230. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1231. status = 0;
  1232. bail:
  1233. if (rename_lock)
  1234. ocfs2_rename_unlock(osb);
  1235. if (handle)
  1236. ocfs2_commit_trans(handle);
  1237. if (orphan_dir) {
  1238. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1239. ocfs2_meta_unlock(orphan_dir, 1);
  1240. mutex_unlock(&orphan_dir->i_mutex);
  1241. iput(orphan_dir);
  1242. }
  1243. if (new_inode)
  1244. sync_mapping_buffers(old_inode->i_mapping);
  1245. if (new_inode)
  1246. iput(new_inode);
  1247. if (newfe_bh)
  1248. brelse(newfe_bh);
  1249. if (old_dir_bh)
  1250. brelse(old_dir_bh);
  1251. if (new_dir_bh)
  1252. brelse(new_dir_bh);
  1253. if (new_de_bh)
  1254. brelse(new_de_bh);
  1255. if (old_de_bh)
  1256. brelse(old_de_bh);
  1257. if (old_inode_de_bh)
  1258. brelse(old_inode_de_bh);
  1259. if (orphan_entry_bh)
  1260. brelse(orphan_entry_bh);
  1261. if (insert_entry_bh)
  1262. brelse(insert_entry_bh);
  1263. mlog_exit(status);
  1264. return status;
  1265. }
  1266. /*
  1267. * we expect i_size = strlen(symname). Copy symname into the file
  1268. * data, including the null terminator.
  1269. */
  1270. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1271. struct ocfs2_journal_handle *handle,
  1272. struct inode *inode,
  1273. const char *symname)
  1274. {
  1275. struct buffer_head **bhs = NULL;
  1276. const char *c;
  1277. struct super_block *sb = osb->sb;
  1278. u64 p_blkno;
  1279. int p_blocks;
  1280. int virtual, blocks, status, i, bytes_left;
  1281. bytes_left = i_size_read(inode) + 1;
  1282. /* we can't trust i_blocks because we're actually going to
  1283. * write i_size + 1 bytes. */
  1284. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1285. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1286. (unsigned long long)inode->i_blocks,
  1287. i_size_read(inode), blocks);
  1288. /* Sanity check -- make sure we're going to fit. */
  1289. if (bytes_left >
  1290. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1291. status = -EIO;
  1292. mlog_errno(status);
  1293. goto bail;
  1294. }
  1295. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1296. if (!bhs) {
  1297. status = -ENOMEM;
  1298. mlog_errno(status);
  1299. goto bail;
  1300. }
  1301. status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
  1302. &p_blocks);
  1303. if (status < 0) {
  1304. mlog_errno(status);
  1305. goto bail;
  1306. }
  1307. /* links can never be larger than one cluster so we know this
  1308. * is all going to be contiguous, but do a sanity check
  1309. * anyway. */
  1310. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1311. status = -EIO;
  1312. mlog_errno(status);
  1313. goto bail;
  1314. }
  1315. virtual = 0;
  1316. while(bytes_left > 0) {
  1317. c = &symname[virtual * sb->s_blocksize];
  1318. bhs[virtual] = sb_getblk(sb, p_blkno);
  1319. if (!bhs[virtual]) {
  1320. status = -ENOMEM;
  1321. mlog_errno(status);
  1322. goto bail;
  1323. }
  1324. ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
  1325. status = ocfs2_journal_access(handle, inode, bhs[virtual],
  1326. OCFS2_JOURNAL_ACCESS_CREATE);
  1327. if (status < 0) {
  1328. mlog_errno(status);
  1329. goto bail;
  1330. }
  1331. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1332. memcpy(bhs[virtual]->b_data, c,
  1333. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1334. bytes_left);
  1335. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1336. if (status < 0) {
  1337. mlog_errno(status);
  1338. goto bail;
  1339. }
  1340. virtual++;
  1341. p_blkno++;
  1342. bytes_left -= sb->s_blocksize;
  1343. }
  1344. status = 0;
  1345. bail:
  1346. if (bhs) {
  1347. for(i = 0; i < blocks; i++)
  1348. if (bhs[i])
  1349. brelse(bhs[i]);
  1350. kfree(bhs);
  1351. }
  1352. mlog_exit(status);
  1353. return status;
  1354. }
  1355. static int ocfs2_symlink(struct inode *dir,
  1356. struct dentry *dentry,
  1357. const char *symname)
  1358. {
  1359. int status, l, credits;
  1360. u64 newsize;
  1361. struct ocfs2_super *osb = NULL;
  1362. struct inode *inode = NULL;
  1363. struct super_block *sb;
  1364. struct buffer_head *new_fe_bh = NULL;
  1365. struct buffer_head *de_bh = NULL;
  1366. struct buffer_head *parent_fe_bh = NULL;
  1367. struct ocfs2_dinode *fe = NULL;
  1368. struct ocfs2_dinode *dirfe;
  1369. struct ocfs2_journal_handle *handle = NULL;
  1370. struct ocfs2_alloc_context *inode_ac = NULL;
  1371. struct ocfs2_alloc_context *data_ac = NULL;
  1372. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1373. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1374. sb = dir->i_sb;
  1375. osb = OCFS2_SB(sb);
  1376. l = strlen(symname) + 1;
  1377. credits = ocfs2_calc_symlink_credits(sb);
  1378. handle = ocfs2_alloc_handle(osb);
  1379. if (handle == NULL) {
  1380. status = -ENOMEM;
  1381. mlog_errno(status);
  1382. goto bail;
  1383. }
  1384. /* lock the parent directory */
  1385. status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
  1386. if (status < 0) {
  1387. if (status != -ENOENT)
  1388. mlog_errno(status);
  1389. goto bail;
  1390. }
  1391. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1392. if (!dirfe->i_links_count) {
  1393. /* can't make a file in a deleted directory. */
  1394. status = -ENOENT;
  1395. goto bail;
  1396. }
  1397. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1398. dentry->d_name.len);
  1399. if (status)
  1400. goto bail;
  1401. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1402. dentry->d_name.name,
  1403. dentry->d_name.len, &de_bh);
  1404. if (status < 0) {
  1405. mlog_errno(status);
  1406. goto bail;
  1407. }
  1408. status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
  1409. if (status < 0) {
  1410. if (status != -ENOSPC)
  1411. mlog_errno(status);
  1412. goto bail;
  1413. }
  1414. /* don't reserve bitmap space for fast symlinks. */
  1415. if (l > ocfs2_fast_symlink_chars(sb)) {
  1416. status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
  1417. if (status < 0) {
  1418. if (status != -ENOSPC)
  1419. mlog_errno(status);
  1420. goto bail;
  1421. }
  1422. }
  1423. handle = ocfs2_start_trans(osb, handle, credits);
  1424. if (IS_ERR(handle)) {
  1425. status = PTR_ERR(handle);
  1426. handle = NULL;
  1427. mlog_errno(status);
  1428. goto bail;
  1429. }
  1430. status = ocfs2_mknod_locked(osb, dir, dentry,
  1431. S_IFLNK | S_IRWXUGO, 0,
  1432. &new_fe_bh, parent_fe_bh, handle,
  1433. &inode, inode_ac);
  1434. if (status < 0) {
  1435. mlog_errno(status);
  1436. goto bail;
  1437. }
  1438. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1439. inode->i_rdev = 0;
  1440. newsize = l - 1;
  1441. if (l > ocfs2_fast_symlink_chars(sb)) {
  1442. inode->i_op = &ocfs2_symlink_inode_operations;
  1443. status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
  1444. handle, data_ac, NULL,
  1445. NULL);
  1446. if (status < 0) {
  1447. if (status != -ENOSPC && status != -EINTR) {
  1448. mlog(ML_ERROR,
  1449. "Failed to extend file to %llu\n",
  1450. (unsigned long long)newsize);
  1451. mlog_errno(status);
  1452. status = -ENOSPC;
  1453. }
  1454. goto bail;
  1455. }
  1456. i_size_write(inode, newsize);
  1457. inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
  1458. } else {
  1459. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1460. memcpy((char *) fe->id2.i_symlink, symname, l);
  1461. i_size_write(inode, newsize);
  1462. inode->i_blocks = 0;
  1463. }
  1464. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1465. if (status < 0) {
  1466. mlog_errno(status);
  1467. goto bail;
  1468. }
  1469. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1470. status = ocfs2_create_symlink_data(osb, handle, inode,
  1471. symname);
  1472. if (status < 0) {
  1473. mlog_errno(status);
  1474. goto bail;
  1475. }
  1476. }
  1477. status = ocfs2_add_entry(handle, dentry, inode,
  1478. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1479. de_bh);
  1480. if (status < 0) {
  1481. mlog_errno(status);
  1482. goto bail;
  1483. }
  1484. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1485. if (status) {
  1486. mlog_errno(status);
  1487. goto bail;
  1488. }
  1489. insert_inode_hash(inode);
  1490. dentry->d_op = &ocfs2_dentry_ops;
  1491. d_instantiate(dentry, inode);
  1492. bail:
  1493. if (handle)
  1494. ocfs2_commit_trans(handle);
  1495. if (new_fe_bh)
  1496. brelse(new_fe_bh);
  1497. if (parent_fe_bh)
  1498. brelse(parent_fe_bh);
  1499. if (de_bh)
  1500. brelse(de_bh);
  1501. if (inode_ac)
  1502. ocfs2_free_alloc_context(inode_ac);
  1503. if (data_ac)
  1504. ocfs2_free_alloc_context(data_ac);
  1505. if ((status < 0) && inode)
  1506. iput(inode);
  1507. mlog_exit(status);
  1508. return status;
  1509. }
  1510. int ocfs2_check_dir_entry(struct inode * dir,
  1511. struct ocfs2_dir_entry * de,
  1512. struct buffer_head * bh,
  1513. unsigned long offset)
  1514. {
  1515. const char *error_msg = NULL;
  1516. const int rlen = le16_to_cpu(de->rec_len);
  1517. if (rlen < OCFS2_DIR_REC_LEN(1))
  1518. error_msg = "rec_len is smaller than minimal";
  1519. else if (rlen % 4 != 0)
  1520. error_msg = "rec_len % 4 != 0";
  1521. else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
  1522. error_msg = "rec_len is too small for name_len";
  1523. else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
  1524. error_msg = "directory entry across blocks";
  1525. if (error_msg != NULL)
  1526. mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  1527. "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
  1528. (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  1529. offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  1530. de->name_len);
  1531. return error_msg == NULL ? 1 : 0;
  1532. }
  1533. /* we don't always have a dentry for what we want to add, so people
  1534. * like orphan dir can call this instead.
  1535. *
  1536. * If you pass me insert_bh, I'll skip the search of the other dir
  1537. * blocks and put the record in there.
  1538. */
  1539. static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
  1540. struct inode *dir,
  1541. const char *name, int namelen,
  1542. struct inode *inode, u64 blkno,
  1543. struct buffer_head *parent_fe_bh,
  1544. struct buffer_head *insert_bh)
  1545. {
  1546. unsigned long offset;
  1547. unsigned short rec_len;
  1548. struct ocfs2_dir_entry *de, *de1;
  1549. struct super_block *sb;
  1550. int retval, status;
  1551. mlog_entry_void();
  1552. sb = dir->i_sb;
  1553. if (!namelen)
  1554. return -EINVAL;
  1555. rec_len = OCFS2_DIR_REC_LEN(namelen);
  1556. offset = 0;
  1557. de = (struct ocfs2_dir_entry *) insert_bh->b_data;
  1558. while (1) {
  1559. BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
  1560. /* These checks should've already been passed by the
  1561. * prepare function, but I guess we can leave them
  1562. * here anyway. */
  1563. if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  1564. retval = -ENOENT;
  1565. goto bail;
  1566. }
  1567. if (ocfs2_match(namelen, name, de)) {
  1568. retval = -EEXIST;
  1569. goto bail;
  1570. }
  1571. if (((le64_to_cpu(de->inode) == 0) &&
  1572. (le16_to_cpu(de->rec_len) >= rec_len)) ||
  1573. (le16_to_cpu(de->rec_len) >=
  1574. (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
  1575. status = ocfs2_journal_access(handle, dir, insert_bh,
  1576. OCFS2_JOURNAL_ACCESS_WRITE);
  1577. /* By now the buffer is marked for journaling */
  1578. offset += le16_to_cpu(de->rec_len);
  1579. if (le64_to_cpu(de->inode)) {
  1580. de1 = (struct ocfs2_dir_entry *)((char *) de +
  1581. OCFS2_DIR_REC_LEN(de->name_len));
  1582. de1->rec_len =
  1583. cpu_to_le16(le16_to_cpu(de->rec_len) -
  1584. OCFS2_DIR_REC_LEN(de->name_len));
  1585. de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1586. de = de1;
  1587. }
  1588. de->file_type = OCFS2_FT_UNKNOWN;
  1589. if (blkno) {
  1590. de->inode = cpu_to_le64(blkno);
  1591. ocfs2_set_de_type(de, inode->i_mode);
  1592. } else
  1593. de->inode = 0;
  1594. de->name_len = namelen;
  1595. memcpy(de->name, name, namelen);
  1596. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  1597. dir->i_version++;
  1598. status = ocfs2_journal_dirty(handle, insert_bh);
  1599. retval = 0;
  1600. goto bail;
  1601. }
  1602. offset += le16_to_cpu(de->rec_len);
  1603. de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  1604. }
  1605. /* when you think about it, the assert above should prevent us
  1606. * from ever getting here. */
  1607. retval = -ENOSPC;
  1608. bail:
  1609. mlog_exit(retval);
  1610. return retval;
  1611. }
  1612. /*
  1613. * ocfs2_delete_entry deletes a directory entry by merging it with the
  1614. * previous entry
  1615. */
  1616. static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
  1617. struct inode *dir,
  1618. struct ocfs2_dir_entry *de_del,
  1619. struct buffer_head *bh)
  1620. {
  1621. struct ocfs2_dir_entry *de, *pde;
  1622. int i, status = -ENOENT;
  1623. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
  1624. i = 0;
  1625. pde = NULL;
  1626. de = (struct ocfs2_dir_entry *) bh->b_data;
  1627. while (i < bh->b_size) {
  1628. if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  1629. status = -EIO;
  1630. mlog_errno(status);
  1631. goto bail;
  1632. }
  1633. if (de == de_del) {
  1634. status = ocfs2_journal_access(handle, dir, bh,
  1635. OCFS2_JOURNAL_ACCESS_WRITE);
  1636. if (status < 0) {
  1637. status = -EIO;
  1638. mlog_errno(status);
  1639. goto bail;
  1640. }
  1641. if (pde)
  1642. pde->rec_len =
  1643. cpu_to_le16(le16_to_cpu(pde->rec_len) +
  1644. le16_to_cpu(de->rec_len));
  1645. else
  1646. de->inode = 0;
  1647. dir->i_version++;
  1648. status = ocfs2_journal_dirty(handle, bh);
  1649. goto bail;
  1650. }
  1651. i += le16_to_cpu(de->rec_len);
  1652. pde = de;
  1653. de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  1654. }
  1655. bail:
  1656. mlog_exit(status);
  1657. return status;
  1658. }
  1659. /*
  1660. * Returns 0 if not found, -1 on failure, and 1 on success
  1661. */
  1662. static int inline ocfs2_search_dirblock(struct buffer_head *bh,
  1663. struct inode *dir,
  1664. const char *name, int namelen,
  1665. unsigned long offset,
  1666. struct ocfs2_dir_entry **res_dir)
  1667. {
  1668. struct ocfs2_dir_entry *de;
  1669. char *dlimit, *de_buf;
  1670. int de_len;
  1671. int ret = 0;
  1672. mlog_entry_void();
  1673. de_buf = bh->b_data;
  1674. dlimit = de_buf + dir->i_sb->s_blocksize;
  1675. while (de_buf < dlimit) {
  1676. /* this code is executed quadratically often */
  1677. /* do minimal checking `by hand' */
  1678. de = (struct ocfs2_dir_entry *) de_buf;
  1679. if (de_buf + namelen <= dlimit &&
  1680. ocfs2_match(namelen, name, de)) {
  1681. /* found a match - just to be sure, do a full check */
  1682. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  1683. ret = -1;
  1684. goto bail;
  1685. }
  1686. *res_dir = de;
  1687. ret = 1;
  1688. goto bail;
  1689. }
  1690. /* prevent looping on a bad block */
  1691. de_len = le16_to_cpu(de->rec_len);
  1692. if (de_len <= 0) {
  1693. ret = -1;
  1694. goto bail;
  1695. }
  1696. de_buf += de_len;
  1697. offset += de_len;
  1698. }
  1699. bail:
  1700. mlog_exit(ret);
  1701. return ret;
  1702. }
  1703. struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
  1704. struct inode *dir,
  1705. struct ocfs2_dir_entry **res_dir)
  1706. {
  1707. struct super_block *sb;
  1708. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  1709. struct buffer_head *bh, *ret = NULL;
  1710. unsigned long start, block, b;
  1711. int ra_max = 0; /* Number of bh's in the readahead
  1712. buffer, bh_use[] */
  1713. int ra_ptr = 0; /* Current index into readahead
  1714. buffer */
  1715. int num = 0;
  1716. int nblocks, i, err;
  1717. mlog_entry_void();
  1718. *res_dir = NULL;
  1719. sb = dir->i_sb;
  1720. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  1721. start = OCFS2_I(dir)->ip_dir_start_lookup;
  1722. if (start >= nblocks)
  1723. start = 0;
  1724. block = start;
  1725. restart:
  1726. do {
  1727. /*
  1728. * We deal with the read-ahead logic here.
  1729. */
  1730. if (ra_ptr >= ra_max) {
  1731. /* Refill the readahead buffer */
  1732. ra_ptr = 0;
  1733. b = block;
  1734. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  1735. /*
  1736. * Terminate if we reach the end of the
  1737. * directory and must wrap, or if our
  1738. * search has finished at this block.
  1739. */
  1740. if (b >= nblocks || (num && block == start)) {
  1741. bh_use[ra_max] = NULL;
  1742. break;
  1743. }
  1744. num++;
  1745. bh = ocfs2_bread(dir, b++, &err, 1);
  1746. bh_use[ra_max] = bh;
  1747. }
  1748. }
  1749. if ((bh = bh_use[ra_ptr++]) == NULL)
  1750. goto next;
  1751. wait_on_buffer(bh);
  1752. if (!buffer_uptodate(bh)) {
  1753. /* read error, skip block & hope for the best */
  1754. ocfs2_error(dir->i_sb, "reading directory %llu, "
  1755. "offset %lu\n",
  1756. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  1757. block);
  1758. brelse(bh);
  1759. goto next;
  1760. }
  1761. i = ocfs2_search_dirblock(bh, dir, name, namelen,
  1762. block << sb->s_blocksize_bits,
  1763. res_dir);
  1764. if (i == 1) {
  1765. OCFS2_I(dir)->ip_dir_start_lookup = block;
  1766. ret = bh;
  1767. goto cleanup_and_exit;
  1768. } else {
  1769. brelse(bh);
  1770. if (i < 0)
  1771. goto cleanup_and_exit;
  1772. }
  1773. next:
  1774. if (++block >= nblocks)
  1775. block = 0;
  1776. } while (block != start);
  1777. /*
  1778. * If the directory has grown while we were searching, then
  1779. * search the last part of the directory before giving up.
  1780. */
  1781. block = nblocks;
  1782. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  1783. if (block < nblocks) {
  1784. start = 0;
  1785. goto restart;
  1786. }
  1787. cleanup_and_exit:
  1788. /* Clean up the read-ahead blocks */
  1789. for (; ra_ptr < ra_max; ra_ptr++)
  1790. brelse(bh_use[ra_ptr]);
  1791. mlog_exit_ptr(ret);
  1792. return ret;
  1793. }
  1794. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1795. {
  1796. int status, namelen;
  1797. mlog_entry_void();
  1798. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1799. (long long)blkno);
  1800. if (namelen <= 0) {
  1801. if (namelen)
  1802. status = namelen;
  1803. else
  1804. status = -EINVAL;
  1805. mlog_errno(status);
  1806. goto bail;
  1807. }
  1808. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1809. status = -EINVAL;
  1810. mlog_errno(status);
  1811. goto bail;
  1812. }
  1813. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1814. namelen);
  1815. status = 0;
  1816. bail:
  1817. mlog_exit(status);
  1818. return status;
  1819. }
  1820. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1821. struct inode **ret_orphan_dir,
  1822. struct inode *inode,
  1823. char *name,
  1824. struct buffer_head **de_bh)
  1825. {
  1826. struct inode *orphan_dir_inode;
  1827. struct buffer_head *orphan_dir_bh = NULL;
  1828. int status = 0;
  1829. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1830. if (status < 0) {
  1831. mlog_errno(status);
  1832. return status;
  1833. }
  1834. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1835. ORPHAN_DIR_SYSTEM_INODE,
  1836. osb->slot_num);
  1837. if (!orphan_dir_inode) {
  1838. status = -ENOENT;
  1839. mlog_errno(status);
  1840. return status;
  1841. }
  1842. mutex_lock(&orphan_dir_inode->i_mutex);
  1843. status = ocfs2_meta_lock(orphan_dir_inode, NULL, &orphan_dir_bh, 1);
  1844. if (status < 0) {
  1845. mlog_errno(status);
  1846. goto leave;
  1847. }
  1848. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1849. orphan_dir_bh, name,
  1850. OCFS2_ORPHAN_NAMELEN, de_bh);
  1851. if (status < 0) {
  1852. ocfs2_meta_unlock(orphan_dir_inode, 1);
  1853. mlog_errno(status);
  1854. goto leave;
  1855. }
  1856. *ret_orphan_dir = orphan_dir_inode;
  1857. leave:
  1858. if (status) {
  1859. mutex_unlock(&orphan_dir_inode->i_mutex);
  1860. iput(orphan_dir_inode);
  1861. }
  1862. if (orphan_dir_bh)
  1863. brelse(orphan_dir_bh);
  1864. mlog_exit(status);
  1865. return status;
  1866. }
  1867. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1868. struct ocfs2_journal_handle *handle,
  1869. struct inode *inode,
  1870. struct ocfs2_dinode *fe,
  1871. char *name,
  1872. struct buffer_head *de_bh,
  1873. struct inode *orphan_dir_inode)
  1874. {
  1875. struct buffer_head *orphan_dir_bh = NULL;
  1876. int status = 0;
  1877. struct ocfs2_dinode *orphan_fe;
  1878. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  1879. status = ocfs2_read_block(osb,
  1880. OCFS2_I(orphan_dir_inode)->ip_blkno,
  1881. &orphan_dir_bh, OCFS2_BH_CACHED,
  1882. orphan_dir_inode);
  1883. if (status < 0) {
  1884. mlog_errno(status);
  1885. goto leave;
  1886. }
  1887. status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
  1888. OCFS2_JOURNAL_ACCESS_WRITE);
  1889. if (status < 0) {
  1890. mlog_errno(status);
  1891. goto leave;
  1892. }
  1893. /* we're a cluster, and nlink can change on disk from
  1894. * underneath us... */
  1895. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1896. if (S_ISDIR(inode->i_mode))
  1897. le16_add_cpu(&orphan_fe->i_links_count, 1);
  1898. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1899. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1900. if (status < 0) {
  1901. mlog_errno(status);
  1902. goto leave;
  1903. }
  1904. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1905. OCFS2_ORPHAN_NAMELEN, inode,
  1906. OCFS2_I(inode)->ip_blkno,
  1907. orphan_dir_bh, de_bh);
  1908. if (status < 0) {
  1909. mlog_errno(status);
  1910. goto leave;
  1911. }
  1912. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1913. /* Record which orphan dir our inode now resides
  1914. * in. delete_inode will use this to determine which orphan
  1915. * dir to lock. */
  1916. spin_lock(&OCFS2_I(inode)->ip_lock);
  1917. OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
  1918. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1919. mlog(0, "Inode %llu orphaned in slot %d\n",
  1920. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1921. leave:
  1922. if (orphan_dir_bh)
  1923. brelse(orphan_dir_bh);
  1924. mlog_exit(status);
  1925. return status;
  1926. }
  1927. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1928. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1929. struct ocfs2_journal_handle *handle,
  1930. struct inode *orphan_dir_inode,
  1931. struct inode *inode,
  1932. struct buffer_head *orphan_dir_bh)
  1933. {
  1934. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1935. struct ocfs2_dinode *orphan_fe;
  1936. int status = 0;
  1937. struct buffer_head *target_de_bh = NULL;
  1938. struct ocfs2_dir_entry *target_de = NULL;
  1939. mlog_entry_void();
  1940. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1941. if (status < 0) {
  1942. mlog_errno(status);
  1943. goto leave;
  1944. }
  1945. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1946. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1947. OCFS2_ORPHAN_NAMELEN);
  1948. /* find it's spot in the orphan directory */
  1949. target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
  1950. orphan_dir_inode, &target_de);
  1951. if (!target_de_bh) {
  1952. status = -ENOENT;
  1953. mlog_errno(status);
  1954. goto leave;
  1955. }
  1956. /* remove it from the orphan directory */
  1957. status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
  1958. target_de_bh);
  1959. if (status < 0) {
  1960. mlog_errno(status);
  1961. goto leave;
  1962. }
  1963. status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
  1964. OCFS2_JOURNAL_ACCESS_WRITE);
  1965. if (status < 0) {
  1966. mlog_errno(status);
  1967. goto leave;
  1968. }
  1969. /* do the i_nlink dance! :) */
  1970. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1971. if (S_ISDIR(inode->i_mode))
  1972. le16_add_cpu(&orphan_fe->i_links_count, -1);
  1973. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1974. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1975. if (status < 0) {
  1976. mlog_errno(status);
  1977. goto leave;
  1978. }
  1979. leave:
  1980. if (target_de_bh)
  1981. brelse(target_de_bh);
  1982. mlog_exit(status);
  1983. return status;
  1984. }
  1985. struct inode_operations ocfs2_dir_iops = {
  1986. .create = ocfs2_create,
  1987. .lookup = ocfs2_lookup,
  1988. .link = ocfs2_link,
  1989. .unlink = ocfs2_unlink,
  1990. .rmdir = ocfs2_unlink,
  1991. .symlink = ocfs2_symlink,
  1992. .mkdir = ocfs2_mkdir,
  1993. .mknod = ocfs2_mknod,
  1994. .rename = ocfs2_rename,
  1995. .setattr = ocfs2_setattr,
  1996. .getattr = ocfs2_getattr,
  1997. };