namei.c 56 KB

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