file.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c
  5. *
  6. * File open, close, extend, truncate
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/capability.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/uio.h>
  32. #include <linux/sched.h>
  33. #include <linux/splice.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #include <linux/falloc.h>
  37. #define MLOG_MASK_PREFIX ML_INODE
  38. #include <cluster/masklog.h>
  39. #include "ocfs2.h"
  40. #include "alloc.h"
  41. #include "aops.h"
  42. #include "dir.h"
  43. #include "dlmglue.h"
  44. #include "extent_map.h"
  45. #include "file.h"
  46. #include "sysfile.h"
  47. #include "inode.h"
  48. #include "ioctl.h"
  49. #include "journal.h"
  50. #include "locks.h"
  51. #include "mmap.h"
  52. #include "suballoc.h"
  53. #include "super.h"
  54. #include "xattr.h"
  55. #include "acl.h"
  56. #include "buffer_head_io.h"
  57. static int ocfs2_sync_inode(struct inode *inode)
  58. {
  59. filemap_fdatawrite(inode->i_mapping);
  60. return sync_mapping_buffers(inode->i_mapping);
  61. }
  62. static int ocfs2_init_file_private(struct inode *inode, struct file *file)
  63. {
  64. struct ocfs2_file_private *fp;
  65. fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
  66. if (!fp)
  67. return -ENOMEM;
  68. fp->fp_file = file;
  69. mutex_init(&fp->fp_mutex);
  70. ocfs2_file_lock_res_init(&fp->fp_flock, fp);
  71. file->private_data = fp;
  72. return 0;
  73. }
  74. static void ocfs2_free_file_private(struct inode *inode, struct file *file)
  75. {
  76. struct ocfs2_file_private *fp = file->private_data;
  77. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  78. if (fp) {
  79. ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
  80. ocfs2_lock_res_free(&fp->fp_flock);
  81. kfree(fp);
  82. file->private_data = NULL;
  83. }
  84. }
  85. static int ocfs2_file_open(struct inode *inode, struct file *file)
  86. {
  87. int status;
  88. int mode = file->f_flags;
  89. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  90. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  91. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  92. spin_lock(&oi->ip_lock);
  93. /* Check that the inode hasn't been wiped from disk by another
  94. * node. If it hasn't then we're safe as long as we hold the
  95. * spin lock until our increment of open count. */
  96. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  97. spin_unlock(&oi->ip_lock);
  98. status = -ENOENT;
  99. goto leave;
  100. }
  101. if (mode & O_DIRECT)
  102. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  103. oi->ip_open_count++;
  104. spin_unlock(&oi->ip_lock);
  105. status = ocfs2_init_file_private(inode, file);
  106. if (status) {
  107. /*
  108. * We want to set open count back if we're failing the
  109. * open.
  110. */
  111. spin_lock(&oi->ip_lock);
  112. oi->ip_open_count--;
  113. spin_unlock(&oi->ip_lock);
  114. }
  115. leave:
  116. mlog_exit(status);
  117. return status;
  118. }
  119. static int ocfs2_file_release(struct inode *inode, struct file *file)
  120. {
  121. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  122. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  123. file->f_path.dentry->d_name.len,
  124. file->f_path.dentry->d_name.name);
  125. spin_lock(&oi->ip_lock);
  126. if (!--oi->ip_open_count)
  127. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  128. spin_unlock(&oi->ip_lock);
  129. ocfs2_free_file_private(inode, file);
  130. mlog_exit(0);
  131. return 0;
  132. }
  133. static int ocfs2_dir_open(struct inode *inode, struct file *file)
  134. {
  135. return ocfs2_init_file_private(inode, file);
  136. }
  137. static int ocfs2_dir_release(struct inode *inode, struct file *file)
  138. {
  139. ocfs2_free_file_private(inode, file);
  140. return 0;
  141. }
  142. static int ocfs2_sync_file(struct file *file,
  143. struct dentry *dentry,
  144. int datasync)
  145. {
  146. int err = 0;
  147. journal_t *journal;
  148. struct inode *inode = dentry->d_inode;
  149. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  150. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  151. dentry->d_name.len, dentry->d_name.name);
  152. err = ocfs2_sync_inode(dentry->d_inode);
  153. if (err)
  154. goto bail;
  155. journal = osb->journal->j_journal;
  156. err = jbd2_journal_force_commit(journal);
  157. bail:
  158. mlog_exit(err);
  159. return (err < 0) ? -EIO : 0;
  160. }
  161. int ocfs2_should_update_atime(struct inode *inode,
  162. struct vfsmount *vfsmnt)
  163. {
  164. struct timespec now;
  165. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  166. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  167. return 0;
  168. if ((inode->i_flags & S_NOATIME) ||
  169. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  170. return 0;
  171. /*
  172. * We can be called with no vfsmnt structure - NFSD will
  173. * sometimes do this.
  174. *
  175. * Note that our action here is different than touch_atime() -
  176. * if we can't tell whether this is a noatime mount, then we
  177. * don't know whether to trust the value of s_atime_quantum.
  178. */
  179. if (vfsmnt == NULL)
  180. return 0;
  181. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  182. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  183. return 0;
  184. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  185. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  186. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  187. return 1;
  188. return 0;
  189. }
  190. now = CURRENT_TIME;
  191. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  192. return 0;
  193. else
  194. return 1;
  195. }
  196. int ocfs2_update_inode_atime(struct inode *inode,
  197. struct buffer_head *bh)
  198. {
  199. int ret;
  200. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  201. handle_t *handle;
  202. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  203. mlog_entry_void();
  204. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  205. if (IS_ERR(handle)) {
  206. ret = PTR_ERR(handle);
  207. mlog_errno(ret);
  208. goto out;
  209. }
  210. ret = ocfs2_journal_access(handle, inode, bh,
  211. OCFS2_JOURNAL_ACCESS_WRITE);
  212. if (ret) {
  213. mlog_errno(ret);
  214. goto out_commit;
  215. }
  216. /*
  217. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  218. * have i_mutex to guard against concurrent changes to other
  219. * inode fields.
  220. */
  221. inode->i_atime = CURRENT_TIME;
  222. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  223. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  224. ret = ocfs2_journal_dirty(handle, bh);
  225. if (ret < 0)
  226. mlog_errno(ret);
  227. out_commit:
  228. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  229. out:
  230. mlog_exit(ret);
  231. return ret;
  232. }
  233. static int ocfs2_set_inode_size(handle_t *handle,
  234. struct inode *inode,
  235. struct buffer_head *fe_bh,
  236. u64 new_i_size)
  237. {
  238. int status;
  239. mlog_entry_void();
  240. i_size_write(inode, new_i_size);
  241. inode->i_blocks = ocfs2_inode_sector_count(inode);
  242. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  243. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  244. if (status < 0) {
  245. mlog_errno(status);
  246. goto bail;
  247. }
  248. bail:
  249. mlog_exit(status);
  250. return status;
  251. }
  252. static int ocfs2_simple_size_update(struct inode *inode,
  253. struct buffer_head *di_bh,
  254. u64 new_i_size)
  255. {
  256. int ret;
  257. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  258. handle_t *handle = NULL;
  259. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  260. if (IS_ERR(handle)) {
  261. ret = PTR_ERR(handle);
  262. mlog_errno(ret);
  263. goto out;
  264. }
  265. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  266. new_i_size);
  267. if (ret < 0)
  268. mlog_errno(ret);
  269. ocfs2_commit_trans(osb, handle);
  270. out:
  271. return ret;
  272. }
  273. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  274. struct inode *inode,
  275. struct buffer_head *fe_bh,
  276. u64 new_i_size)
  277. {
  278. int status;
  279. handle_t *handle;
  280. struct ocfs2_dinode *di;
  281. u64 cluster_bytes;
  282. mlog_entry_void();
  283. /* TODO: This needs to actually orphan the inode in this
  284. * transaction. */
  285. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  286. if (IS_ERR(handle)) {
  287. status = PTR_ERR(handle);
  288. mlog_errno(status);
  289. goto out;
  290. }
  291. status = ocfs2_journal_access(handle, inode, fe_bh,
  292. OCFS2_JOURNAL_ACCESS_WRITE);
  293. if (status < 0) {
  294. mlog_errno(status);
  295. goto out_commit;
  296. }
  297. /*
  298. * Do this before setting i_size.
  299. */
  300. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  301. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  302. cluster_bytes);
  303. if (status) {
  304. mlog_errno(status);
  305. goto out_commit;
  306. }
  307. i_size_write(inode, new_i_size);
  308. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  309. di = (struct ocfs2_dinode *) fe_bh->b_data;
  310. di->i_size = cpu_to_le64(new_i_size);
  311. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  312. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  313. status = ocfs2_journal_dirty(handle, fe_bh);
  314. if (status < 0)
  315. mlog_errno(status);
  316. out_commit:
  317. ocfs2_commit_trans(osb, handle);
  318. out:
  319. mlog_exit(status);
  320. return status;
  321. }
  322. static int ocfs2_truncate_file(struct inode *inode,
  323. struct buffer_head *di_bh,
  324. u64 new_i_size)
  325. {
  326. int status = 0;
  327. struct ocfs2_dinode *fe = NULL;
  328. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  329. struct ocfs2_truncate_context *tc = NULL;
  330. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  331. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  332. (unsigned long long)new_i_size);
  333. /* We trust di_bh because it comes from ocfs2_inode_lock(), which
  334. * already validated it */
  335. fe = (struct ocfs2_dinode *) di_bh->b_data;
  336. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  337. "Inode %llu, inode i_size = %lld != di "
  338. "i_size = %llu, i_flags = 0x%x\n",
  339. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  340. i_size_read(inode),
  341. (unsigned long long)le64_to_cpu(fe->i_size),
  342. le32_to_cpu(fe->i_flags));
  343. if (new_i_size > le64_to_cpu(fe->i_size)) {
  344. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  345. (unsigned long long)le64_to_cpu(fe->i_size),
  346. (unsigned long long)new_i_size);
  347. status = -EINVAL;
  348. mlog_errno(status);
  349. goto bail;
  350. }
  351. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  352. (unsigned long long)le64_to_cpu(fe->i_blkno),
  353. (unsigned long long)le64_to_cpu(fe->i_size),
  354. (unsigned long long)new_i_size);
  355. /* lets handle the simple truncate cases before doing any more
  356. * cluster locking. */
  357. if (new_i_size == le64_to_cpu(fe->i_size))
  358. goto bail;
  359. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  360. /*
  361. * The inode lock forced other nodes to sync and drop their
  362. * pages, which (correctly) happens even if we have a truncate
  363. * without allocation change - ocfs2 cluster sizes can be much
  364. * greater than page size, so we have to truncate them
  365. * anyway.
  366. */
  367. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  368. truncate_inode_pages(inode->i_mapping, new_i_size);
  369. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  370. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  371. i_size_read(inode), 1);
  372. if (status)
  373. mlog_errno(status);
  374. goto bail_unlock_sem;
  375. }
  376. /* alright, we're going to need to do a full blown alloc size
  377. * change. Orphan the inode so that recovery can complete the
  378. * truncate if necessary. This does the task of marking
  379. * i_size. */
  380. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  381. if (status < 0) {
  382. mlog_errno(status);
  383. goto bail_unlock_sem;
  384. }
  385. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  386. if (status < 0) {
  387. mlog_errno(status);
  388. goto bail_unlock_sem;
  389. }
  390. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  391. if (status < 0) {
  392. mlog_errno(status);
  393. goto bail_unlock_sem;
  394. }
  395. /* TODO: orphan dir cleanup here. */
  396. bail_unlock_sem:
  397. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  398. bail:
  399. mlog_exit(status);
  400. return status;
  401. }
  402. /*
  403. * extend file allocation only here.
  404. * we'll update all the disk stuff, and oip->alloc_size
  405. *
  406. * expect stuff to be locked, a transaction started and enough data /
  407. * metadata reservations in the contexts.
  408. *
  409. * Will return -EAGAIN, and a reason if a restart is needed.
  410. * If passed in, *reason will always be set, even in error.
  411. */
  412. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  413. struct inode *inode,
  414. u32 *logical_offset,
  415. u32 clusters_to_add,
  416. int mark_unwritten,
  417. struct buffer_head *fe_bh,
  418. handle_t *handle,
  419. struct ocfs2_alloc_context *data_ac,
  420. struct ocfs2_alloc_context *meta_ac,
  421. enum ocfs2_alloc_restarted *reason_ret)
  422. {
  423. int ret;
  424. struct ocfs2_extent_tree et;
  425. ocfs2_init_dinode_extent_tree(&et, inode, fe_bh);
  426. ret = ocfs2_add_clusters_in_btree(osb, inode, logical_offset,
  427. clusters_to_add, mark_unwritten,
  428. &et, handle,
  429. data_ac, meta_ac, reason_ret);
  430. return ret;
  431. }
  432. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  433. u32 clusters_to_add, int mark_unwritten)
  434. {
  435. int status = 0;
  436. int restart_func = 0;
  437. int credits;
  438. u32 prev_clusters;
  439. struct buffer_head *bh = NULL;
  440. struct ocfs2_dinode *fe = NULL;
  441. handle_t *handle = NULL;
  442. struct ocfs2_alloc_context *data_ac = NULL;
  443. struct ocfs2_alloc_context *meta_ac = NULL;
  444. enum ocfs2_alloc_restarted why;
  445. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  446. struct ocfs2_extent_tree et;
  447. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  448. /*
  449. * This function only exists for file systems which don't
  450. * support holes.
  451. */
  452. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  453. status = ocfs2_read_inode_block(inode, &bh);
  454. if (status < 0) {
  455. mlog_errno(status);
  456. goto leave;
  457. }
  458. fe = (struct ocfs2_dinode *) bh->b_data;
  459. restart_all:
  460. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  461. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  462. "clusters_to_add = %u\n",
  463. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  464. (long long)i_size_read(inode), le32_to_cpu(fe->i_clusters),
  465. clusters_to_add);
  466. ocfs2_init_dinode_extent_tree(&et, inode, bh);
  467. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  468. &data_ac, &meta_ac);
  469. if (status) {
  470. mlog_errno(status);
  471. goto leave;
  472. }
  473. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
  474. clusters_to_add);
  475. handle = ocfs2_start_trans(osb, credits);
  476. if (IS_ERR(handle)) {
  477. status = PTR_ERR(handle);
  478. handle = NULL;
  479. mlog_errno(status);
  480. goto leave;
  481. }
  482. restarted_transaction:
  483. /* reserve a write to the file entry early on - that we if we
  484. * run out of credits in the allocation path, we can still
  485. * update i_size. */
  486. status = ocfs2_journal_access(handle, inode, bh,
  487. OCFS2_JOURNAL_ACCESS_WRITE);
  488. if (status < 0) {
  489. mlog_errno(status);
  490. goto leave;
  491. }
  492. prev_clusters = OCFS2_I(inode)->ip_clusters;
  493. status = ocfs2_add_inode_data(osb,
  494. inode,
  495. &logical_start,
  496. clusters_to_add,
  497. mark_unwritten,
  498. bh,
  499. handle,
  500. data_ac,
  501. meta_ac,
  502. &why);
  503. if ((status < 0) && (status != -EAGAIN)) {
  504. if (status != -ENOSPC)
  505. mlog_errno(status);
  506. goto leave;
  507. }
  508. status = ocfs2_journal_dirty(handle, bh);
  509. if (status < 0) {
  510. mlog_errno(status);
  511. goto leave;
  512. }
  513. spin_lock(&OCFS2_I(inode)->ip_lock);
  514. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  515. spin_unlock(&OCFS2_I(inode)->ip_lock);
  516. if (why != RESTART_NONE && clusters_to_add) {
  517. if (why == RESTART_META) {
  518. mlog(0, "restarting function.\n");
  519. restart_func = 1;
  520. } else {
  521. BUG_ON(why != RESTART_TRANS);
  522. mlog(0, "restarting transaction.\n");
  523. /* TODO: This can be more intelligent. */
  524. credits = ocfs2_calc_extend_credits(osb->sb,
  525. &fe->id2.i_list,
  526. clusters_to_add);
  527. status = ocfs2_extend_trans(handle, credits);
  528. if (status < 0) {
  529. /* handle still has to be committed at
  530. * this point. */
  531. status = -ENOMEM;
  532. mlog_errno(status);
  533. goto leave;
  534. }
  535. goto restarted_transaction;
  536. }
  537. }
  538. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  539. le32_to_cpu(fe->i_clusters),
  540. (unsigned long long)le64_to_cpu(fe->i_size));
  541. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  542. OCFS2_I(inode)->ip_clusters, (long long)i_size_read(inode));
  543. leave:
  544. if (handle) {
  545. ocfs2_commit_trans(osb, handle);
  546. handle = NULL;
  547. }
  548. if (data_ac) {
  549. ocfs2_free_alloc_context(data_ac);
  550. data_ac = NULL;
  551. }
  552. if (meta_ac) {
  553. ocfs2_free_alloc_context(meta_ac);
  554. meta_ac = NULL;
  555. }
  556. if ((!status) && restart_func) {
  557. restart_func = 0;
  558. goto restart_all;
  559. }
  560. brelse(bh);
  561. bh = NULL;
  562. mlog_exit(status);
  563. return status;
  564. }
  565. /* Some parts of this taken from generic_cont_expand, which turned out
  566. * to be too fragile to do exactly what we need without us having to
  567. * worry about recursive locking in ->write_begin() and ->write_end(). */
  568. static int ocfs2_write_zero_page(struct inode *inode,
  569. u64 size)
  570. {
  571. struct address_space *mapping = inode->i_mapping;
  572. struct page *page;
  573. unsigned long index;
  574. unsigned int offset;
  575. handle_t *handle = NULL;
  576. int ret;
  577. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  578. /* ugh. in prepare/commit_write, if from==to==start of block, we
  579. ** skip the prepare. make sure we never send an offset for the start
  580. ** of a block
  581. */
  582. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  583. offset++;
  584. }
  585. index = size >> PAGE_CACHE_SHIFT;
  586. page = grab_cache_page(mapping, index);
  587. if (!page) {
  588. ret = -ENOMEM;
  589. mlog_errno(ret);
  590. goto out;
  591. }
  592. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  593. if (ret < 0) {
  594. mlog_errno(ret);
  595. goto out_unlock;
  596. }
  597. if (ocfs2_should_order_data(inode)) {
  598. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  599. offset);
  600. if (IS_ERR(handle)) {
  601. ret = PTR_ERR(handle);
  602. handle = NULL;
  603. goto out_unlock;
  604. }
  605. }
  606. /* must not update i_size! */
  607. ret = block_commit_write(page, offset, offset);
  608. if (ret < 0)
  609. mlog_errno(ret);
  610. else
  611. ret = 0;
  612. if (handle)
  613. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  614. out_unlock:
  615. unlock_page(page);
  616. page_cache_release(page);
  617. out:
  618. return ret;
  619. }
  620. static int ocfs2_zero_extend(struct inode *inode,
  621. u64 zero_to_size)
  622. {
  623. int ret = 0;
  624. u64 start_off;
  625. struct super_block *sb = inode->i_sb;
  626. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  627. while (start_off < zero_to_size) {
  628. ret = ocfs2_write_zero_page(inode, start_off);
  629. if (ret < 0) {
  630. mlog_errno(ret);
  631. goto out;
  632. }
  633. start_off += sb->s_blocksize;
  634. /*
  635. * Very large extends have the potential to lock up
  636. * the cpu for extended periods of time.
  637. */
  638. cond_resched();
  639. }
  640. out:
  641. return ret;
  642. }
  643. int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
  644. {
  645. int ret;
  646. u32 clusters_to_add;
  647. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  648. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  649. if (clusters_to_add < oi->ip_clusters)
  650. clusters_to_add = 0;
  651. else
  652. clusters_to_add -= oi->ip_clusters;
  653. if (clusters_to_add) {
  654. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  655. clusters_to_add, 0);
  656. if (ret) {
  657. mlog_errno(ret);
  658. goto out;
  659. }
  660. }
  661. /*
  662. * Call this even if we don't add any clusters to the tree. We
  663. * still need to zero the area between the old i_size and the
  664. * new i_size.
  665. */
  666. ret = ocfs2_zero_extend(inode, zero_to);
  667. if (ret < 0)
  668. mlog_errno(ret);
  669. out:
  670. return ret;
  671. }
  672. static int ocfs2_extend_file(struct inode *inode,
  673. struct buffer_head *di_bh,
  674. u64 new_i_size)
  675. {
  676. int ret = 0;
  677. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  678. BUG_ON(!di_bh);
  679. /* setattr sometimes calls us like this. */
  680. if (new_i_size == 0)
  681. goto out;
  682. if (i_size_read(inode) == new_i_size)
  683. goto out;
  684. BUG_ON(new_i_size < i_size_read(inode));
  685. /*
  686. * Fall through for converting inline data, even if the fs
  687. * supports sparse files.
  688. *
  689. * The check for inline data here is legal - nobody can add
  690. * the feature since we have i_mutex. We must check it again
  691. * after acquiring ip_alloc_sem though, as paths like mmap
  692. * might have raced us to converting the inode to extents.
  693. */
  694. if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  695. && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  696. goto out_update_size;
  697. /*
  698. * The alloc sem blocks people in read/write from reading our
  699. * allocation until we're done changing it. We depend on
  700. * i_mutex to block other extend/truncate calls while we're
  701. * here.
  702. */
  703. down_write(&oi->ip_alloc_sem);
  704. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  705. /*
  706. * We can optimize small extends by keeping the inodes
  707. * inline data.
  708. */
  709. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  710. up_write(&oi->ip_alloc_sem);
  711. goto out_update_size;
  712. }
  713. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  714. if (ret) {
  715. up_write(&oi->ip_alloc_sem);
  716. mlog_errno(ret);
  717. goto out;
  718. }
  719. }
  720. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  721. ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
  722. up_write(&oi->ip_alloc_sem);
  723. if (ret < 0) {
  724. mlog_errno(ret);
  725. goto out;
  726. }
  727. out_update_size:
  728. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  729. if (ret < 0)
  730. mlog_errno(ret);
  731. out:
  732. return ret;
  733. }
  734. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  735. {
  736. int status = 0, size_change;
  737. struct inode *inode = dentry->d_inode;
  738. struct super_block *sb = inode->i_sb;
  739. struct ocfs2_super *osb = OCFS2_SB(sb);
  740. struct buffer_head *bh = NULL;
  741. handle_t *handle = NULL;
  742. mlog_entry("(0x%p, '%.*s')\n", dentry,
  743. dentry->d_name.len, dentry->d_name.name);
  744. /* ensuring we don't even attempt to truncate a symlink */
  745. if (S_ISLNK(inode->i_mode))
  746. attr->ia_valid &= ~ATTR_SIZE;
  747. if (attr->ia_valid & ATTR_MODE)
  748. mlog(0, "mode change: %d\n", attr->ia_mode);
  749. if (attr->ia_valid & ATTR_UID)
  750. mlog(0, "uid change: %d\n", attr->ia_uid);
  751. if (attr->ia_valid & ATTR_GID)
  752. mlog(0, "gid change: %d\n", attr->ia_gid);
  753. if (attr->ia_valid & ATTR_SIZE)
  754. mlog(0, "size change...\n");
  755. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  756. mlog(0, "time change...\n");
  757. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  758. | ATTR_GID | ATTR_UID | ATTR_MODE)
  759. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  760. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  761. return 0;
  762. }
  763. status = inode_change_ok(inode, attr);
  764. if (status)
  765. return status;
  766. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  767. if (size_change) {
  768. status = ocfs2_rw_lock(inode, 1);
  769. if (status < 0) {
  770. mlog_errno(status);
  771. goto bail;
  772. }
  773. }
  774. status = ocfs2_inode_lock(inode, &bh, 1);
  775. if (status < 0) {
  776. if (status != -ENOENT)
  777. mlog_errno(status);
  778. goto bail_unlock_rw;
  779. }
  780. if (size_change && attr->ia_size != i_size_read(inode)) {
  781. if (attr->ia_size > sb->s_maxbytes) {
  782. status = -EFBIG;
  783. goto bail_unlock;
  784. }
  785. if (i_size_read(inode) > attr->ia_size) {
  786. if (ocfs2_should_order_data(inode)) {
  787. status = ocfs2_begin_ordered_truncate(inode,
  788. attr->ia_size);
  789. if (status)
  790. goto bail_unlock;
  791. }
  792. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  793. } else
  794. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  795. if (status < 0) {
  796. if (status != -ENOSPC)
  797. mlog_errno(status);
  798. status = -ENOSPC;
  799. goto bail_unlock;
  800. }
  801. }
  802. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  803. if (IS_ERR(handle)) {
  804. status = PTR_ERR(handle);
  805. mlog_errno(status);
  806. goto bail_unlock;
  807. }
  808. /*
  809. * This will intentionally not wind up calling vmtruncate(),
  810. * since all the work for a size change has been done above.
  811. * Otherwise, we could get into problems with truncate as
  812. * ip_alloc_sem is used there to protect against i_size
  813. * changes.
  814. */
  815. status = inode_setattr(inode, attr);
  816. if (status < 0) {
  817. mlog_errno(status);
  818. goto bail_commit;
  819. }
  820. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  821. if (status < 0)
  822. mlog_errno(status);
  823. bail_commit:
  824. ocfs2_commit_trans(osb, handle);
  825. bail_unlock:
  826. ocfs2_inode_unlock(inode, 1);
  827. bail_unlock_rw:
  828. if (size_change)
  829. ocfs2_rw_unlock(inode, 1);
  830. bail:
  831. brelse(bh);
  832. if (!status && attr->ia_valid & ATTR_MODE) {
  833. status = ocfs2_acl_chmod(inode);
  834. if (status < 0)
  835. mlog_errno(status);
  836. }
  837. mlog_exit(status);
  838. return status;
  839. }
  840. int ocfs2_getattr(struct vfsmount *mnt,
  841. struct dentry *dentry,
  842. struct kstat *stat)
  843. {
  844. struct inode *inode = dentry->d_inode;
  845. struct super_block *sb = dentry->d_inode->i_sb;
  846. struct ocfs2_super *osb = sb->s_fs_info;
  847. int err;
  848. mlog_entry_void();
  849. err = ocfs2_inode_revalidate(dentry);
  850. if (err) {
  851. if (err != -ENOENT)
  852. mlog_errno(err);
  853. goto bail;
  854. }
  855. generic_fillattr(inode, stat);
  856. /* We set the blksize from the cluster size for performance */
  857. stat->blksize = osb->s_clustersize;
  858. bail:
  859. mlog_exit(err);
  860. return err;
  861. }
  862. int ocfs2_permission(struct inode *inode, int mask)
  863. {
  864. int ret;
  865. mlog_entry_void();
  866. ret = ocfs2_inode_lock(inode, NULL, 0);
  867. if (ret) {
  868. if (ret != -ENOENT)
  869. mlog_errno(ret);
  870. goto out;
  871. }
  872. ret = generic_permission(inode, mask, ocfs2_check_acl);
  873. ocfs2_inode_unlock(inode, 0);
  874. out:
  875. mlog_exit(ret);
  876. return ret;
  877. }
  878. static int __ocfs2_write_remove_suid(struct inode *inode,
  879. struct buffer_head *bh)
  880. {
  881. int ret;
  882. handle_t *handle;
  883. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  884. struct ocfs2_dinode *di;
  885. mlog_entry("(Inode %llu, mode 0%o)\n",
  886. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  887. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  888. if (IS_ERR(handle)) {
  889. ret = PTR_ERR(handle);
  890. mlog_errno(ret);
  891. goto out;
  892. }
  893. ret = ocfs2_journal_access(handle, inode, bh,
  894. OCFS2_JOURNAL_ACCESS_WRITE);
  895. if (ret < 0) {
  896. mlog_errno(ret);
  897. goto out_trans;
  898. }
  899. inode->i_mode &= ~S_ISUID;
  900. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  901. inode->i_mode &= ~S_ISGID;
  902. di = (struct ocfs2_dinode *) bh->b_data;
  903. di->i_mode = cpu_to_le16(inode->i_mode);
  904. ret = ocfs2_journal_dirty(handle, bh);
  905. if (ret < 0)
  906. mlog_errno(ret);
  907. out_trans:
  908. ocfs2_commit_trans(osb, handle);
  909. out:
  910. mlog_exit(ret);
  911. return ret;
  912. }
  913. /*
  914. * Will look for holes and unwritten extents in the range starting at
  915. * pos for count bytes (inclusive).
  916. */
  917. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  918. size_t count)
  919. {
  920. int ret = 0;
  921. unsigned int extent_flags;
  922. u32 cpos, clusters, extent_len, phys_cpos;
  923. struct super_block *sb = inode->i_sb;
  924. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  925. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  926. while (clusters) {
  927. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  928. &extent_flags);
  929. if (ret < 0) {
  930. mlog_errno(ret);
  931. goto out;
  932. }
  933. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  934. ret = 1;
  935. break;
  936. }
  937. if (extent_len > clusters)
  938. extent_len = clusters;
  939. clusters -= extent_len;
  940. cpos += extent_len;
  941. }
  942. out:
  943. return ret;
  944. }
  945. static int ocfs2_write_remove_suid(struct inode *inode)
  946. {
  947. int ret;
  948. struct buffer_head *bh = NULL;
  949. ret = ocfs2_read_inode_block(inode, &bh);
  950. if (ret < 0) {
  951. mlog_errno(ret);
  952. goto out;
  953. }
  954. ret = __ocfs2_write_remove_suid(inode, bh);
  955. out:
  956. brelse(bh);
  957. return ret;
  958. }
  959. /*
  960. * Allocate enough extents to cover the region starting at byte offset
  961. * start for len bytes. Existing extents are skipped, any extents
  962. * added are marked as "unwritten".
  963. */
  964. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  965. u64 start, u64 len)
  966. {
  967. int ret;
  968. u32 cpos, phys_cpos, clusters, alloc_size;
  969. u64 end = start + len;
  970. struct buffer_head *di_bh = NULL;
  971. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  972. ret = ocfs2_read_inode_block(inode, &di_bh);
  973. if (ret) {
  974. mlog_errno(ret);
  975. goto out;
  976. }
  977. /*
  978. * Nothing to do if the requested reservation range
  979. * fits within the inode.
  980. */
  981. if (ocfs2_size_fits_inline_data(di_bh, end))
  982. goto out;
  983. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  984. if (ret) {
  985. mlog_errno(ret);
  986. goto out;
  987. }
  988. }
  989. /*
  990. * We consider both start and len to be inclusive.
  991. */
  992. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  993. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  994. clusters -= cpos;
  995. while (clusters) {
  996. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  997. &alloc_size, NULL);
  998. if (ret) {
  999. mlog_errno(ret);
  1000. goto out;
  1001. }
  1002. /*
  1003. * Hole or existing extent len can be arbitrary, so
  1004. * cap it to our own allocation request.
  1005. */
  1006. if (alloc_size > clusters)
  1007. alloc_size = clusters;
  1008. if (phys_cpos) {
  1009. /*
  1010. * We already have an allocation at this
  1011. * region so we can safely skip it.
  1012. */
  1013. goto next;
  1014. }
  1015. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1016. if (ret) {
  1017. if (ret != -ENOSPC)
  1018. mlog_errno(ret);
  1019. goto out;
  1020. }
  1021. next:
  1022. cpos += alloc_size;
  1023. clusters -= alloc_size;
  1024. }
  1025. ret = 0;
  1026. out:
  1027. brelse(di_bh);
  1028. return ret;
  1029. }
  1030. /*
  1031. * Truncate a byte range, avoiding pages within partial clusters. This
  1032. * preserves those pages for the zeroing code to write to.
  1033. */
  1034. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1035. u64 byte_len)
  1036. {
  1037. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1038. loff_t start, end;
  1039. struct address_space *mapping = inode->i_mapping;
  1040. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1041. end = byte_start + byte_len;
  1042. end = end & ~(osb->s_clustersize - 1);
  1043. if (start < end) {
  1044. unmap_mapping_range(mapping, start, end - start, 0);
  1045. truncate_inode_pages_range(mapping, start, end - 1);
  1046. }
  1047. }
  1048. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1049. u64 start, u64 len)
  1050. {
  1051. int ret = 0;
  1052. u64 tmpend, end = start + len;
  1053. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1054. unsigned int csize = osb->s_clustersize;
  1055. handle_t *handle;
  1056. /*
  1057. * The "start" and "end" values are NOT necessarily part of
  1058. * the range whose allocation is being deleted. Rather, this
  1059. * is what the user passed in with the request. We must zero
  1060. * partial clusters here. There's no need to worry about
  1061. * physical allocation - the zeroing code knows to skip holes.
  1062. */
  1063. mlog(0, "byte start: %llu, end: %llu\n",
  1064. (unsigned long long)start, (unsigned long long)end);
  1065. /*
  1066. * If both edges are on a cluster boundary then there's no
  1067. * zeroing required as the region is part of the allocation to
  1068. * be truncated.
  1069. */
  1070. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1071. goto out;
  1072. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1073. if (IS_ERR(handle)) {
  1074. ret = PTR_ERR(handle);
  1075. mlog_errno(ret);
  1076. goto out;
  1077. }
  1078. /*
  1079. * We want to get the byte offset of the end of the 1st cluster.
  1080. */
  1081. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1082. if (tmpend > end)
  1083. tmpend = end;
  1084. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1085. (unsigned long long)start, (unsigned long long)tmpend);
  1086. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1087. if (ret)
  1088. mlog_errno(ret);
  1089. if (tmpend < end) {
  1090. /*
  1091. * This may make start and end equal, but the zeroing
  1092. * code will skip any work in that case so there's no
  1093. * need to catch it up here.
  1094. */
  1095. start = end & ~(osb->s_clustersize - 1);
  1096. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1097. (unsigned long long)start, (unsigned long long)end);
  1098. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1099. if (ret)
  1100. mlog_errno(ret);
  1101. }
  1102. ocfs2_commit_trans(osb, handle);
  1103. out:
  1104. return ret;
  1105. }
  1106. static int ocfs2_remove_inode_range(struct inode *inode,
  1107. struct buffer_head *di_bh, u64 byte_start,
  1108. u64 byte_len)
  1109. {
  1110. int ret = 0;
  1111. u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
  1112. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1113. struct ocfs2_cached_dealloc_ctxt dealloc;
  1114. struct address_space *mapping = inode->i_mapping;
  1115. struct ocfs2_extent_tree et;
  1116. ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
  1117. ocfs2_init_dealloc_ctxt(&dealloc);
  1118. if (byte_len == 0)
  1119. return 0;
  1120. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1121. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1122. byte_start + byte_len, 0);
  1123. if (ret) {
  1124. mlog_errno(ret);
  1125. goto out;
  1126. }
  1127. /*
  1128. * There's no need to get fancy with the page cache
  1129. * truncate of an inline-data inode. We're talking
  1130. * about less than a page here, which will be cached
  1131. * in the dinode buffer anyway.
  1132. */
  1133. unmap_mapping_range(mapping, 0, 0, 0);
  1134. truncate_inode_pages(mapping, 0);
  1135. goto out;
  1136. }
  1137. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1138. trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1139. if (trunc_len >= trunc_start)
  1140. trunc_len -= trunc_start;
  1141. else
  1142. trunc_len = 0;
  1143. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
  1144. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1145. (unsigned long long)byte_start,
  1146. (unsigned long long)byte_len, trunc_start, trunc_len);
  1147. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1148. if (ret) {
  1149. mlog_errno(ret);
  1150. goto out;
  1151. }
  1152. cpos = trunc_start;
  1153. while (trunc_len) {
  1154. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1155. &alloc_size, NULL);
  1156. if (ret) {
  1157. mlog_errno(ret);
  1158. goto out;
  1159. }
  1160. if (alloc_size > trunc_len)
  1161. alloc_size = trunc_len;
  1162. /* Only do work for non-holes */
  1163. if (phys_cpos != 0) {
  1164. ret = ocfs2_remove_btree_range(inode, &et, cpos,
  1165. phys_cpos, alloc_size,
  1166. &dealloc);
  1167. if (ret) {
  1168. mlog_errno(ret);
  1169. goto out;
  1170. }
  1171. }
  1172. cpos += alloc_size;
  1173. trunc_len -= alloc_size;
  1174. }
  1175. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1176. out:
  1177. ocfs2_schedule_truncate_log_flush(osb, 1);
  1178. ocfs2_run_deallocs(osb, &dealloc);
  1179. return ret;
  1180. }
  1181. /*
  1182. * Parts of this function taken from xfs_change_file_space()
  1183. */
  1184. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1185. loff_t f_pos, unsigned int cmd,
  1186. struct ocfs2_space_resv *sr,
  1187. int change_size)
  1188. {
  1189. int ret;
  1190. s64 llen;
  1191. loff_t size;
  1192. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1193. struct buffer_head *di_bh = NULL;
  1194. handle_t *handle;
  1195. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1196. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1197. return -EROFS;
  1198. mutex_lock(&inode->i_mutex);
  1199. /*
  1200. * This prevents concurrent writes on other nodes
  1201. */
  1202. ret = ocfs2_rw_lock(inode, 1);
  1203. if (ret) {
  1204. mlog_errno(ret);
  1205. goto out;
  1206. }
  1207. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1208. if (ret) {
  1209. mlog_errno(ret);
  1210. goto out_rw_unlock;
  1211. }
  1212. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1213. ret = -EPERM;
  1214. goto out_inode_unlock;
  1215. }
  1216. switch (sr->l_whence) {
  1217. case 0: /*SEEK_SET*/
  1218. break;
  1219. case 1: /*SEEK_CUR*/
  1220. sr->l_start += f_pos;
  1221. break;
  1222. case 2: /*SEEK_END*/
  1223. sr->l_start += i_size_read(inode);
  1224. break;
  1225. default:
  1226. ret = -EINVAL;
  1227. goto out_inode_unlock;
  1228. }
  1229. sr->l_whence = 0;
  1230. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1231. if (sr->l_start < 0
  1232. || sr->l_start > max_off
  1233. || (sr->l_start + llen) < 0
  1234. || (sr->l_start + llen) > max_off) {
  1235. ret = -EINVAL;
  1236. goto out_inode_unlock;
  1237. }
  1238. size = sr->l_start + sr->l_len;
  1239. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1240. if (sr->l_len <= 0) {
  1241. ret = -EINVAL;
  1242. goto out_inode_unlock;
  1243. }
  1244. }
  1245. if (file && should_remove_suid(file->f_path.dentry)) {
  1246. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1247. if (ret) {
  1248. mlog_errno(ret);
  1249. goto out_inode_unlock;
  1250. }
  1251. }
  1252. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1253. switch (cmd) {
  1254. case OCFS2_IOC_RESVSP:
  1255. case OCFS2_IOC_RESVSP64:
  1256. /*
  1257. * This takes unsigned offsets, but the signed ones we
  1258. * pass have been checked against overflow above.
  1259. */
  1260. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1261. sr->l_len);
  1262. break;
  1263. case OCFS2_IOC_UNRESVSP:
  1264. case OCFS2_IOC_UNRESVSP64:
  1265. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1266. sr->l_len);
  1267. break;
  1268. default:
  1269. ret = -EINVAL;
  1270. }
  1271. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1272. if (ret) {
  1273. mlog_errno(ret);
  1274. goto out_inode_unlock;
  1275. }
  1276. /*
  1277. * We update c/mtime for these changes
  1278. */
  1279. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1280. if (IS_ERR(handle)) {
  1281. ret = PTR_ERR(handle);
  1282. mlog_errno(ret);
  1283. goto out_inode_unlock;
  1284. }
  1285. if (change_size && i_size_read(inode) < size)
  1286. i_size_write(inode, size);
  1287. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1288. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1289. if (ret < 0)
  1290. mlog_errno(ret);
  1291. ocfs2_commit_trans(osb, handle);
  1292. out_inode_unlock:
  1293. brelse(di_bh);
  1294. ocfs2_inode_unlock(inode, 1);
  1295. out_rw_unlock:
  1296. ocfs2_rw_unlock(inode, 1);
  1297. out:
  1298. mutex_unlock(&inode->i_mutex);
  1299. return ret;
  1300. }
  1301. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1302. struct ocfs2_space_resv *sr)
  1303. {
  1304. struct inode *inode = file->f_path.dentry->d_inode;
  1305. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
  1306. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1307. !ocfs2_writes_unwritten_extents(osb))
  1308. return -ENOTTY;
  1309. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1310. !ocfs2_sparse_alloc(osb))
  1311. return -ENOTTY;
  1312. if (!S_ISREG(inode->i_mode))
  1313. return -EINVAL;
  1314. if (!(file->f_mode & FMODE_WRITE))
  1315. return -EBADF;
  1316. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1317. }
  1318. static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1319. loff_t len)
  1320. {
  1321. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1322. struct ocfs2_space_resv sr;
  1323. int change_size = 1;
  1324. if (!ocfs2_writes_unwritten_extents(osb))
  1325. return -EOPNOTSUPP;
  1326. if (S_ISDIR(inode->i_mode))
  1327. return -ENODEV;
  1328. if (mode & FALLOC_FL_KEEP_SIZE)
  1329. change_size = 0;
  1330. sr.l_whence = 0;
  1331. sr.l_start = (s64)offset;
  1332. sr.l_len = (s64)len;
  1333. return __ocfs2_change_file_space(NULL, inode, offset,
  1334. OCFS2_IOC_RESVSP64, &sr, change_size);
  1335. }
  1336. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1337. loff_t *ppos,
  1338. size_t count,
  1339. int appending,
  1340. int *direct_io)
  1341. {
  1342. int ret = 0, meta_level = 0;
  1343. struct inode *inode = dentry->d_inode;
  1344. loff_t saved_pos, end;
  1345. /*
  1346. * We start with a read level meta lock and only jump to an ex
  1347. * if we need to make modifications here.
  1348. */
  1349. for(;;) {
  1350. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1351. if (ret < 0) {
  1352. meta_level = -1;
  1353. mlog_errno(ret);
  1354. goto out;
  1355. }
  1356. /* Clear suid / sgid if necessary. We do this here
  1357. * instead of later in the write path because
  1358. * remove_suid() calls ->setattr without any hint that
  1359. * we may have already done our cluster locking. Since
  1360. * ocfs2_setattr() *must* take cluster locks to
  1361. * proceeed, this will lead us to recursively lock the
  1362. * inode. There's also the dinode i_size state which
  1363. * can be lost via setattr during extending writes (we
  1364. * set inode->i_size at the end of a write. */
  1365. if (should_remove_suid(dentry)) {
  1366. if (meta_level == 0) {
  1367. ocfs2_inode_unlock(inode, meta_level);
  1368. meta_level = 1;
  1369. continue;
  1370. }
  1371. ret = ocfs2_write_remove_suid(inode);
  1372. if (ret < 0) {
  1373. mlog_errno(ret);
  1374. goto out_unlock;
  1375. }
  1376. }
  1377. /* work on a copy of ppos until we're sure that we won't have
  1378. * to recalculate it due to relocking. */
  1379. if (appending) {
  1380. saved_pos = i_size_read(inode);
  1381. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1382. } else {
  1383. saved_pos = *ppos;
  1384. }
  1385. end = saved_pos + count;
  1386. /*
  1387. * Skip the O_DIRECT checks if we don't need
  1388. * them.
  1389. */
  1390. if (!direct_io || !(*direct_io))
  1391. break;
  1392. /*
  1393. * There's no sane way to do direct writes to an inode
  1394. * with inline data.
  1395. */
  1396. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1397. *direct_io = 0;
  1398. break;
  1399. }
  1400. /*
  1401. * Allowing concurrent direct writes means
  1402. * i_size changes wouldn't be synchronized, so
  1403. * one node could wind up truncating another
  1404. * nodes writes.
  1405. */
  1406. if (end > i_size_read(inode)) {
  1407. *direct_io = 0;
  1408. break;
  1409. }
  1410. /*
  1411. * We don't fill holes during direct io, so
  1412. * check for them here. If any are found, the
  1413. * caller will have to retake some cluster
  1414. * locks and initiate the io as buffered.
  1415. */
  1416. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1417. if (ret == 1) {
  1418. *direct_io = 0;
  1419. ret = 0;
  1420. } else if (ret < 0)
  1421. mlog_errno(ret);
  1422. break;
  1423. }
  1424. if (appending)
  1425. *ppos = saved_pos;
  1426. out_unlock:
  1427. ocfs2_inode_unlock(inode, meta_level);
  1428. out:
  1429. return ret;
  1430. }
  1431. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1432. const struct iovec *iov,
  1433. unsigned long nr_segs,
  1434. loff_t pos)
  1435. {
  1436. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1437. int can_do_direct;
  1438. ssize_t written = 0;
  1439. size_t ocount; /* original count */
  1440. size_t count; /* after file limit checks */
  1441. loff_t old_size, *ppos = &iocb->ki_pos;
  1442. u32 old_clusters;
  1443. struct file *file = iocb->ki_filp;
  1444. struct inode *inode = file->f_path.dentry->d_inode;
  1445. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1446. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1447. (unsigned int)nr_segs,
  1448. file->f_path.dentry->d_name.len,
  1449. file->f_path.dentry->d_name.name);
  1450. if (iocb->ki_left == 0)
  1451. return 0;
  1452. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1453. appending = file->f_flags & O_APPEND ? 1 : 0;
  1454. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1455. mutex_lock(&inode->i_mutex);
  1456. relock:
  1457. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1458. if (direct_io) {
  1459. down_read(&inode->i_alloc_sem);
  1460. have_alloc_sem = 1;
  1461. }
  1462. /* concurrent O_DIRECT writes are allowed */
  1463. rw_level = !direct_io;
  1464. ret = ocfs2_rw_lock(inode, rw_level);
  1465. if (ret < 0) {
  1466. mlog_errno(ret);
  1467. goto out_sems;
  1468. }
  1469. can_do_direct = direct_io;
  1470. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1471. iocb->ki_left, appending,
  1472. &can_do_direct);
  1473. if (ret < 0) {
  1474. mlog_errno(ret);
  1475. goto out;
  1476. }
  1477. /*
  1478. * We can't complete the direct I/O as requested, fall back to
  1479. * buffered I/O.
  1480. */
  1481. if (direct_io && !can_do_direct) {
  1482. ocfs2_rw_unlock(inode, rw_level);
  1483. up_read(&inode->i_alloc_sem);
  1484. have_alloc_sem = 0;
  1485. rw_level = -1;
  1486. direct_io = 0;
  1487. goto relock;
  1488. }
  1489. /*
  1490. * To later detect whether a journal commit for sync writes is
  1491. * necessary, we sample i_size, and cluster count here.
  1492. */
  1493. old_size = i_size_read(inode);
  1494. old_clusters = OCFS2_I(inode)->ip_clusters;
  1495. /* communicate with ocfs2_dio_end_io */
  1496. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1497. if (direct_io) {
  1498. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  1499. VERIFY_READ);
  1500. if (ret)
  1501. goto out_dio;
  1502. ret = generic_write_checks(file, ppos, &count,
  1503. S_ISBLK(inode->i_mode));
  1504. if (ret)
  1505. goto out_dio;
  1506. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1507. ppos, count, ocount);
  1508. if (written < 0) {
  1509. /*
  1510. * direct write may have instantiated a few
  1511. * blocks outside i_size. Trim these off again.
  1512. * Don't need i_size_read because we hold i_mutex.
  1513. */
  1514. if (*ppos + count > inode->i_size)
  1515. vmtruncate(inode, inode->i_size);
  1516. ret = written;
  1517. goto out_dio;
  1518. }
  1519. } else {
  1520. written = generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1521. *ppos);
  1522. }
  1523. out_dio:
  1524. /* buffered aio wouldn't have proper lock coverage today */
  1525. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1526. if ((file->f_flags & O_SYNC && !direct_io) || IS_SYNC(inode)) {
  1527. /*
  1528. * The generic write paths have handled getting data
  1529. * to disk, but since we don't make use of the dirty
  1530. * inode list, a manual journal commit is necessary
  1531. * here.
  1532. */
  1533. if (old_size != i_size_read(inode) ||
  1534. old_clusters != OCFS2_I(inode)->ip_clusters) {
  1535. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  1536. if (ret < 0)
  1537. written = ret;
  1538. }
  1539. }
  1540. /*
  1541. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1542. * function pointer which is called when o_direct io completes so that
  1543. * it can unlock our rw lock. (it's the clustered equivalent of
  1544. * i_alloc_sem; protects truncate from racing with pending ios).
  1545. * Unfortunately there are error cases which call end_io and others
  1546. * that don't. so we don't have to unlock the rw_lock if either an
  1547. * async dio is going to do it in the future or an end_io after an
  1548. * error has already done it.
  1549. */
  1550. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1551. rw_level = -1;
  1552. have_alloc_sem = 0;
  1553. }
  1554. out:
  1555. if (rw_level != -1)
  1556. ocfs2_rw_unlock(inode, rw_level);
  1557. out_sems:
  1558. if (have_alloc_sem)
  1559. up_read(&inode->i_alloc_sem);
  1560. mutex_unlock(&inode->i_mutex);
  1561. mlog_exit(ret);
  1562. return written ? written : ret;
  1563. }
  1564. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1565. struct file *out,
  1566. loff_t *ppos,
  1567. size_t len,
  1568. unsigned int flags)
  1569. {
  1570. int ret;
  1571. struct inode *inode = out->f_path.dentry->d_inode;
  1572. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1573. (unsigned int)len,
  1574. out->f_path.dentry->d_name.len,
  1575. out->f_path.dentry->d_name.name);
  1576. inode_double_lock(inode, pipe->inode);
  1577. ret = ocfs2_rw_lock(inode, 1);
  1578. if (ret < 0) {
  1579. mlog_errno(ret);
  1580. goto out;
  1581. }
  1582. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1583. NULL);
  1584. if (ret < 0) {
  1585. mlog_errno(ret);
  1586. goto out_unlock;
  1587. }
  1588. ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
  1589. out_unlock:
  1590. ocfs2_rw_unlock(inode, 1);
  1591. out:
  1592. inode_double_unlock(inode, pipe->inode);
  1593. mlog_exit(ret);
  1594. return ret;
  1595. }
  1596. static ssize_t ocfs2_file_splice_read(struct file *in,
  1597. loff_t *ppos,
  1598. struct pipe_inode_info *pipe,
  1599. size_t len,
  1600. unsigned int flags)
  1601. {
  1602. int ret = 0;
  1603. struct inode *inode = in->f_path.dentry->d_inode;
  1604. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1605. (unsigned int)len,
  1606. in->f_path.dentry->d_name.len,
  1607. in->f_path.dentry->d_name.name);
  1608. /*
  1609. * See the comment in ocfs2_file_aio_read()
  1610. */
  1611. ret = ocfs2_inode_lock(inode, NULL, 0);
  1612. if (ret < 0) {
  1613. mlog_errno(ret);
  1614. goto bail;
  1615. }
  1616. ocfs2_inode_unlock(inode, 0);
  1617. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1618. bail:
  1619. mlog_exit(ret);
  1620. return ret;
  1621. }
  1622. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1623. const struct iovec *iov,
  1624. unsigned long nr_segs,
  1625. loff_t pos)
  1626. {
  1627. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1628. struct file *filp = iocb->ki_filp;
  1629. struct inode *inode = filp->f_path.dentry->d_inode;
  1630. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1631. (unsigned int)nr_segs,
  1632. filp->f_path.dentry->d_name.len,
  1633. filp->f_path.dentry->d_name.name);
  1634. if (!inode) {
  1635. ret = -EINVAL;
  1636. mlog_errno(ret);
  1637. goto bail;
  1638. }
  1639. /*
  1640. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1641. * need locks to protect pending reads from racing with truncate.
  1642. */
  1643. if (filp->f_flags & O_DIRECT) {
  1644. down_read(&inode->i_alloc_sem);
  1645. have_alloc_sem = 1;
  1646. ret = ocfs2_rw_lock(inode, 0);
  1647. if (ret < 0) {
  1648. mlog_errno(ret);
  1649. goto bail;
  1650. }
  1651. rw_level = 0;
  1652. /* communicate with ocfs2_dio_end_io */
  1653. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1654. }
  1655. /*
  1656. * We're fine letting folks race truncates and extending
  1657. * writes with read across the cluster, just like they can
  1658. * locally. Hence no rw_lock during read.
  1659. *
  1660. * Take and drop the meta data lock to update inode fields
  1661. * like i_size. This allows the checks down below
  1662. * generic_file_aio_read() a chance of actually working.
  1663. */
  1664. ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1665. if (ret < 0) {
  1666. mlog_errno(ret);
  1667. goto bail;
  1668. }
  1669. ocfs2_inode_unlock(inode, lock_level);
  1670. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1671. if (ret == -EINVAL)
  1672. mlog(0, "generic_file_aio_read returned -EINVAL\n");
  1673. /* buffered aio wouldn't have proper lock coverage today */
  1674. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1675. /* see ocfs2_file_aio_write */
  1676. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1677. rw_level = -1;
  1678. have_alloc_sem = 0;
  1679. }
  1680. bail:
  1681. if (have_alloc_sem)
  1682. up_read(&inode->i_alloc_sem);
  1683. if (rw_level != -1)
  1684. ocfs2_rw_unlock(inode, rw_level);
  1685. mlog_exit(ret);
  1686. return ret;
  1687. }
  1688. const struct inode_operations ocfs2_file_iops = {
  1689. .setattr = ocfs2_setattr,
  1690. .getattr = ocfs2_getattr,
  1691. .permission = ocfs2_permission,
  1692. .setxattr = generic_setxattr,
  1693. .getxattr = generic_getxattr,
  1694. .listxattr = ocfs2_listxattr,
  1695. .removexattr = generic_removexattr,
  1696. .fallocate = ocfs2_fallocate,
  1697. .fiemap = ocfs2_fiemap,
  1698. };
  1699. const struct inode_operations ocfs2_special_file_iops = {
  1700. .setattr = ocfs2_setattr,
  1701. .getattr = ocfs2_getattr,
  1702. .permission = ocfs2_permission,
  1703. };
  1704. /*
  1705. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  1706. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  1707. */
  1708. const struct file_operations ocfs2_fops = {
  1709. .llseek = generic_file_llseek,
  1710. .read = do_sync_read,
  1711. .write = do_sync_write,
  1712. .mmap = ocfs2_mmap,
  1713. .fsync = ocfs2_sync_file,
  1714. .release = ocfs2_file_release,
  1715. .open = ocfs2_file_open,
  1716. .aio_read = ocfs2_file_aio_read,
  1717. .aio_write = ocfs2_file_aio_write,
  1718. .unlocked_ioctl = ocfs2_ioctl,
  1719. #ifdef CONFIG_COMPAT
  1720. .compat_ioctl = ocfs2_compat_ioctl,
  1721. #endif
  1722. .lock = ocfs2_lock,
  1723. .flock = ocfs2_flock,
  1724. .splice_read = ocfs2_file_splice_read,
  1725. .splice_write = ocfs2_file_splice_write,
  1726. };
  1727. const struct file_operations ocfs2_dops = {
  1728. .llseek = generic_file_llseek,
  1729. .read = generic_read_dir,
  1730. .readdir = ocfs2_readdir,
  1731. .fsync = ocfs2_sync_file,
  1732. .release = ocfs2_dir_release,
  1733. .open = ocfs2_dir_open,
  1734. .unlocked_ioctl = ocfs2_ioctl,
  1735. #ifdef CONFIG_COMPAT
  1736. .compat_ioctl = ocfs2_compat_ioctl,
  1737. #endif
  1738. .lock = ocfs2_lock,
  1739. .flock = ocfs2_flock,
  1740. };
  1741. /*
  1742. * POSIX-lockless variants of our file_operations.
  1743. *
  1744. * These will be used if the underlying cluster stack does not support
  1745. * posix file locking, if the user passes the "localflocks" mount
  1746. * option, or if we have a local-only fs.
  1747. *
  1748. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  1749. * so we still want it in the case of no stack support for
  1750. * plocks. Internally, it will do the right thing when asked to ignore
  1751. * the cluster.
  1752. */
  1753. const struct file_operations ocfs2_fops_no_plocks = {
  1754. .llseek = generic_file_llseek,
  1755. .read = do_sync_read,
  1756. .write = do_sync_write,
  1757. .mmap = ocfs2_mmap,
  1758. .fsync = ocfs2_sync_file,
  1759. .release = ocfs2_file_release,
  1760. .open = ocfs2_file_open,
  1761. .aio_read = ocfs2_file_aio_read,
  1762. .aio_write = ocfs2_file_aio_write,
  1763. .unlocked_ioctl = ocfs2_ioctl,
  1764. #ifdef CONFIG_COMPAT
  1765. .compat_ioctl = ocfs2_compat_ioctl,
  1766. #endif
  1767. .flock = ocfs2_flock,
  1768. .splice_read = ocfs2_file_splice_read,
  1769. .splice_write = ocfs2_file_splice_write,
  1770. };
  1771. const struct file_operations ocfs2_dops_no_plocks = {
  1772. .llseek = generic_file_llseek,
  1773. .read = generic_read_dir,
  1774. .readdir = ocfs2_readdir,
  1775. .fsync = ocfs2_sync_file,
  1776. .release = ocfs2_dir_release,
  1777. .open = ocfs2_dir_open,
  1778. .unlocked_ioctl = ocfs2_ioctl,
  1779. #ifdef CONFIG_COMPAT
  1780. .compat_ioctl = ocfs2_compat_ioctl,
  1781. #endif
  1782. .flock = ocfs2_flock,
  1783. };