file.c 53 KB

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