file.c 52 KB

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