file.c 51 KB

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