file.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  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. #include <linux/blkdev.h>
  39. #include <linux/backing-dev.h>
  40. #include <cluster/masklog.h>
  41. #include "ocfs2.h"
  42. #include "alloc.h"
  43. #include "aops.h"
  44. #include "dir.h"
  45. #include "dlmglue.h"
  46. #include "extent_map.h"
  47. #include "file.h"
  48. #include "sysfile.h"
  49. #include "inode.h"
  50. #include "ioctl.h"
  51. #include "journal.h"
  52. #include "locks.h"
  53. #include "mmap.h"
  54. #include "suballoc.h"
  55. #include "super.h"
  56. #include "xattr.h"
  57. #include "acl.h"
  58. #include "quota.h"
  59. #include "refcounttree.h"
  60. #include "ocfs2_trace.h"
  61. #include "buffer_head_io.h"
  62. static int ocfs2_init_file_private(struct inode *inode, struct file *file)
  63. {
  64. struct ocfs2_file_private *fp;
  65. fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
  66. if (!fp)
  67. return -ENOMEM;
  68. fp->fp_file = file;
  69. mutex_init(&fp->fp_mutex);
  70. ocfs2_file_lock_res_init(&fp->fp_flock, fp);
  71. file->private_data = fp;
  72. return 0;
  73. }
  74. static void ocfs2_free_file_private(struct inode *inode, struct file *file)
  75. {
  76. struct ocfs2_file_private *fp = file->private_data;
  77. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  78. if (fp) {
  79. ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
  80. ocfs2_lock_res_free(&fp->fp_flock);
  81. kfree(fp);
  82. file->private_data = NULL;
  83. }
  84. }
  85. static int ocfs2_file_open(struct inode *inode, struct file *file)
  86. {
  87. int status;
  88. int mode = file->f_flags;
  89. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  90. trace_ocfs2_file_open(inode, file, file->f_path.dentry,
  91. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  92. file->f_path.dentry->d_name.len,
  93. file->f_path.dentry->d_name.name, mode);
  94. if (file->f_mode & FMODE_WRITE) {
  95. status = dquot_initialize(inode);
  96. if (status)
  97. goto leave;
  98. }
  99. spin_lock(&oi->ip_lock);
  100. /* Check that the inode hasn't been wiped from disk by another
  101. * node. If it hasn't then we're safe as long as we hold the
  102. * spin lock until our increment of open count. */
  103. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  104. spin_unlock(&oi->ip_lock);
  105. status = -ENOENT;
  106. goto leave;
  107. }
  108. if (mode & O_DIRECT)
  109. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  110. oi->ip_open_count++;
  111. spin_unlock(&oi->ip_lock);
  112. status = ocfs2_init_file_private(inode, file);
  113. if (status) {
  114. /*
  115. * We want to set open count back if we're failing the
  116. * open.
  117. */
  118. spin_lock(&oi->ip_lock);
  119. oi->ip_open_count--;
  120. spin_unlock(&oi->ip_lock);
  121. }
  122. leave:
  123. return status;
  124. }
  125. static int ocfs2_file_release(struct inode *inode, struct file *file)
  126. {
  127. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  128. spin_lock(&oi->ip_lock);
  129. if (!--oi->ip_open_count)
  130. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  131. trace_ocfs2_file_release(inode, file, file->f_path.dentry,
  132. oi->ip_blkno,
  133. file->f_path.dentry->d_name.len,
  134. file->f_path.dentry->d_name.name,
  135. oi->ip_open_count);
  136. spin_unlock(&oi->ip_lock);
  137. ocfs2_free_file_private(inode, file);
  138. return 0;
  139. }
  140. static int ocfs2_dir_open(struct inode *inode, struct file *file)
  141. {
  142. return ocfs2_init_file_private(inode, file);
  143. }
  144. static int ocfs2_dir_release(struct inode *inode, struct file *file)
  145. {
  146. ocfs2_free_file_private(inode, file);
  147. return 0;
  148. }
  149. static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
  150. int datasync)
  151. {
  152. int err = 0;
  153. struct inode *inode = file->f_mapping->host;
  154. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  155. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  156. journal_t *journal = osb->journal->j_journal;
  157. int ret;
  158. tid_t commit_tid;
  159. bool needs_barrier = false;
  160. trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
  161. OCFS2_I(inode)->ip_blkno,
  162. file->f_path.dentry->d_name.len,
  163. file->f_path.dentry->d_name.name,
  164. (unsigned long long)datasync);
  165. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  166. return -EROFS;
  167. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  168. if (err)
  169. return err;
  170. commit_tid = datasync ? oi->i_datasync_tid : oi->i_sync_tid;
  171. if (journal->j_flags & JBD2_BARRIER &&
  172. !jbd2_trans_will_send_data_barrier(journal, commit_tid))
  173. needs_barrier = true;
  174. err = jbd2_complete_transaction(journal, commit_tid);
  175. if (needs_barrier) {
  176. ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  177. if (!err)
  178. err = ret;
  179. }
  180. if (err)
  181. mlog_errno(err);
  182. return (err < 0) ? -EIO : 0;
  183. }
  184. int ocfs2_should_update_atime(struct inode *inode,
  185. struct vfsmount *vfsmnt)
  186. {
  187. struct timespec now;
  188. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  189. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  190. return 0;
  191. if ((inode->i_flags & S_NOATIME) ||
  192. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  193. return 0;
  194. /*
  195. * We can be called with no vfsmnt structure - NFSD will
  196. * sometimes do this.
  197. *
  198. * Note that our action here is different than touch_atime() -
  199. * if we can't tell whether this is a noatime mount, then we
  200. * don't know whether to trust the value of s_atime_quantum.
  201. */
  202. if (vfsmnt == NULL)
  203. return 0;
  204. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  205. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  206. return 0;
  207. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  208. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  209. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  210. return 1;
  211. return 0;
  212. }
  213. now = current_time(inode);
  214. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  215. return 0;
  216. else
  217. return 1;
  218. }
  219. int ocfs2_update_inode_atime(struct inode *inode,
  220. struct buffer_head *bh)
  221. {
  222. int ret;
  223. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  224. handle_t *handle;
  225. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  226. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  227. if (IS_ERR(handle)) {
  228. ret = PTR_ERR(handle);
  229. mlog_errno(ret);
  230. goto out;
  231. }
  232. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  233. OCFS2_JOURNAL_ACCESS_WRITE);
  234. if (ret) {
  235. mlog_errno(ret);
  236. goto out_commit;
  237. }
  238. /*
  239. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  240. * have i_mutex to guard against concurrent changes to other
  241. * inode fields.
  242. */
  243. inode->i_atime = current_time(inode);
  244. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  245. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  246. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  247. ocfs2_journal_dirty(handle, bh);
  248. out_commit:
  249. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  250. out:
  251. return ret;
  252. }
  253. int ocfs2_set_inode_size(handle_t *handle,
  254. struct inode *inode,
  255. struct buffer_head *fe_bh,
  256. u64 new_i_size)
  257. {
  258. int status;
  259. i_size_write(inode, new_i_size);
  260. inode->i_blocks = ocfs2_inode_sector_count(inode);
  261. inode->i_ctime = inode->i_mtime = current_time(inode);
  262. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  263. if (status < 0) {
  264. mlog_errno(status);
  265. goto bail;
  266. }
  267. bail:
  268. return status;
  269. }
  270. int ocfs2_simple_size_update(struct inode *inode,
  271. struct buffer_head *di_bh,
  272. u64 new_i_size)
  273. {
  274. int ret;
  275. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  276. handle_t *handle = NULL;
  277. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  278. if (IS_ERR(handle)) {
  279. ret = PTR_ERR(handle);
  280. mlog_errno(ret);
  281. goto out;
  282. }
  283. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  284. new_i_size);
  285. if (ret < 0)
  286. mlog_errno(ret);
  287. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  288. ocfs2_commit_trans(osb, handle);
  289. out:
  290. return ret;
  291. }
  292. static int ocfs2_cow_file_pos(struct inode *inode,
  293. struct buffer_head *fe_bh,
  294. u64 offset)
  295. {
  296. int status;
  297. u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  298. unsigned int num_clusters = 0;
  299. unsigned int ext_flags = 0;
  300. /*
  301. * If the new offset is aligned to the range of the cluster, there is
  302. * no space for ocfs2_zero_range_for_truncate to fill, so no need to
  303. * CoW either.
  304. */
  305. if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
  306. return 0;
  307. status = ocfs2_get_clusters(inode, cpos, &phys,
  308. &num_clusters, &ext_flags);
  309. if (status) {
  310. mlog_errno(status);
  311. goto out;
  312. }
  313. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  314. goto out;
  315. return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
  316. out:
  317. return status;
  318. }
  319. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  320. struct inode *inode,
  321. struct buffer_head *fe_bh,
  322. u64 new_i_size)
  323. {
  324. int status;
  325. handle_t *handle;
  326. struct ocfs2_dinode *di;
  327. u64 cluster_bytes;
  328. /*
  329. * We need to CoW the cluster contains the offset if it is reflinked
  330. * since we will call ocfs2_zero_range_for_truncate later which will
  331. * write "0" from offset to the end of the cluster.
  332. */
  333. status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
  334. if (status) {
  335. mlog_errno(status);
  336. return status;
  337. }
  338. /* TODO: This needs to actually orphan the inode in this
  339. * transaction. */
  340. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  341. if (IS_ERR(handle)) {
  342. status = PTR_ERR(handle);
  343. mlog_errno(status);
  344. goto out;
  345. }
  346. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  347. OCFS2_JOURNAL_ACCESS_WRITE);
  348. if (status < 0) {
  349. mlog_errno(status);
  350. goto out_commit;
  351. }
  352. /*
  353. * Do this before setting i_size.
  354. */
  355. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  356. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  357. cluster_bytes);
  358. if (status) {
  359. mlog_errno(status);
  360. goto out_commit;
  361. }
  362. i_size_write(inode, new_i_size);
  363. inode->i_ctime = inode->i_mtime = current_time(inode);
  364. di = (struct ocfs2_dinode *) fe_bh->b_data;
  365. di->i_size = cpu_to_le64(new_i_size);
  366. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  367. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  368. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  369. ocfs2_journal_dirty(handle, fe_bh);
  370. out_commit:
  371. ocfs2_commit_trans(osb, handle);
  372. out:
  373. return status;
  374. }
  375. int ocfs2_truncate_file(struct inode *inode,
  376. struct buffer_head *di_bh,
  377. u64 new_i_size)
  378. {
  379. int status = 0;
  380. struct ocfs2_dinode *fe = NULL;
  381. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  382. /* We trust di_bh because it comes from ocfs2_inode_lock(), which
  383. * already validated it */
  384. fe = (struct ocfs2_dinode *) di_bh->b_data;
  385. trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno,
  386. (unsigned long long)le64_to_cpu(fe->i_size),
  387. (unsigned long long)new_i_size);
  388. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  389. "Inode %llu, inode i_size = %lld != di "
  390. "i_size = %llu, i_flags = 0x%x\n",
  391. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  392. i_size_read(inode),
  393. (unsigned long long)le64_to_cpu(fe->i_size),
  394. le32_to_cpu(fe->i_flags));
  395. if (new_i_size > le64_to_cpu(fe->i_size)) {
  396. trace_ocfs2_truncate_file_error(
  397. (unsigned long long)le64_to_cpu(fe->i_size),
  398. (unsigned long long)new_i_size);
  399. status = -EINVAL;
  400. mlog_errno(status);
  401. goto bail;
  402. }
  403. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  404. ocfs2_resv_discard(&osb->osb_la_resmap,
  405. &OCFS2_I(inode)->ip_la_data_resv);
  406. /*
  407. * The inode lock forced other nodes to sync and drop their
  408. * pages, which (correctly) happens even if we have a truncate
  409. * without allocation change - ocfs2 cluster sizes can be much
  410. * greater than page size, so we have to truncate them
  411. * anyway.
  412. */
  413. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  414. truncate_inode_pages(inode->i_mapping, new_i_size);
  415. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  416. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  417. i_size_read(inode), 1);
  418. if (status)
  419. mlog_errno(status);
  420. goto bail_unlock_sem;
  421. }
  422. /* alright, we're going to need to do a full blown alloc size
  423. * change. Orphan the inode so that recovery can complete the
  424. * truncate if necessary. This does the task of marking
  425. * i_size. */
  426. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  427. if (status < 0) {
  428. mlog_errno(status);
  429. goto bail_unlock_sem;
  430. }
  431. status = ocfs2_commit_truncate(osb, inode, di_bh);
  432. if (status < 0) {
  433. mlog_errno(status);
  434. goto bail_unlock_sem;
  435. }
  436. /* TODO: orphan dir cleanup here. */
  437. bail_unlock_sem:
  438. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  439. bail:
  440. if (!status && OCFS2_I(inode)->ip_clusters == 0)
  441. status = ocfs2_try_remove_refcount_tree(inode, di_bh);
  442. return status;
  443. }
  444. /*
  445. * extend file allocation only here.
  446. * we'll update all the disk stuff, and oip->alloc_size
  447. *
  448. * expect stuff to be locked, a transaction started and enough data /
  449. * metadata reservations in the contexts.
  450. *
  451. * Will return -EAGAIN, and a reason if a restart is needed.
  452. * If passed in, *reason will always be set, even in error.
  453. */
  454. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  455. struct inode *inode,
  456. u32 *logical_offset,
  457. u32 clusters_to_add,
  458. int mark_unwritten,
  459. struct buffer_head *fe_bh,
  460. handle_t *handle,
  461. struct ocfs2_alloc_context *data_ac,
  462. struct ocfs2_alloc_context *meta_ac,
  463. enum ocfs2_alloc_restarted *reason_ret)
  464. {
  465. int ret;
  466. struct ocfs2_extent_tree et;
  467. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
  468. ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
  469. clusters_to_add, mark_unwritten,
  470. data_ac, meta_ac, reason_ret);
  471. return ret;
  472. }
  473. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  474. u32 clusters_to_add, int mark_unwritten)
  475. {
  476. int status = 0;
  477. int restart_func = 0;
  478. int credits;
  479. u32 prev_clusters;
  480. struct buffer_head *bh = NULL;
  481. struct ocfs2_dinode *fe = NULL;
  482. handle_t *handle = NULL;
  483. struct ocfs2_alloc_context *data_ac = NULL;
  484. struct ocfs2_alloc_context *meta_ac = NULL;
  485. enum ocfs2_alloc_restarted why = RESTART_NONE;
  486. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  487. struct ocfs2_extent_tree et;
  488. int did_quota = 0;
  489. /*
  490. * Unwritten extent only exists for file systems which
  491. * support holes.
  492. */
  493. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  494. status = ocfs2_read_inode_block(inode, &bh);
  495. if (status < 0) {
  496. mlog_errno(status);
  497. goto leave;
  498. }
  499. fe = (struct ocfs2_dinode *) bh->b_data;
  500. restart_all:
  501. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  502. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
  503. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  504. &data_ac, &meta_ac);
  505. if (status) {
  506. mlog_errno(status);
  507. goto leave;
  508. }
  509. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list);
  510. handle = ocfs2_start_trans(osb, credits);
  511. if (IS_ERR(handle)) {
  512. status = PTR_ERR(handle);
  513. handle = NULL;
  514. mlog_errno(status);
  515. goto leave;
  516. }
  517. restarted_transaction:
  518. trace_ocfs2_extend_allocation(
  519. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  520. (unsigned long long)i_size_read(inode),
  521. le32_to_cpu(fe->i_clusters), clusters_to_add,
  522. why, restart_func);
  523. status = dquot_alloc_space_nodirty(inode,
  524. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  525. if (status)
  526. goto leave;
  527. did_quota = 1;
  528. /* reserve a write to the file entry early on - that we if we
  529. * run out of credits in the allocation path, we can still
  530. * update i_size. */
  531. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  532. OCFS2_JOURNAL_ACCESS_WRITE);
  533. if (status < 0) {
  534. mlog_errno(status);
  535. goto leave;
  536. }
  537. prev_clusters = OCFS2_I(inode)->ip_clusters;
  538. status = ocfs2_add_inode_data(osb,
  539. inode,
  540. &logical_start,
  541. clusters_to_add,
  542. mark_unwritten,
  543. bh,
  544. handle,
  545. data_ac,
  546. meta_ac,
  547. &why);
  548. if ((status < 0) && (status != -EAGAIN)) {
  549. if (status != -ENOSPC)
  550. mlog_errno(status);
  551. goto leave;
  552. }
  553. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  554. ocfs2_journal_dirty(handle, bh);
  555. spin_lock(&OCFS2_I(inode)->ip_lock);
  556. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  557. spin_unlock(&OCFS2_I(inode)->ip_lock);
  558. /* Release unused quota reservation */
  559. dquot_free_space(inode,
  560. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  561. did_quota = 0;
  562. if (why != RESTART_NONE && clusters_to_add) {
  563. if (why == RESTART_META) {
  564. restart_func = 1;
  565. status = 0;
  566. } else {
  567. BUG_ON(why != RESTART_TRANS);
  568. status = ocfs2_allocate_extend_trans(handle, 1);
  569. if (status < 0) {
  570. /* handle still has to be committed at
  571. * this point. */
  572. status = -ENOMEM;
  573. mlog_errno(status);
  574. goto leave;
  575. }
  576. goto restarted_transaction;
  577. }
  578. }
  579. trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
  580. le32_to_cpu(fe->i_clusters),
  581. (unsigned long long)le64_to_cpu(fe->i_size),
  582. OCFS2_I(inode)->ip_clusters,
  583. (unsigned long long)i_size_read(inode));
  584. leave:
  585. if (status < 0 && did_quota)
  586. dquot_free_space(inode,
  587. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  588. if (handle) {
  589. ocfs2_commit_trans(osb, handle);
  590. handle = NULL;
  591. }
  592. if (data_ac) {
  593. ocfs2_free_alloc_context(data_ac);
  594. data_ac = NULL;
  595. }
  596. if (meta_ac) {
  597. ocfs2_free_alloc_context(meta_ac);
  598. meta_ac = NULL;
  599. }
  600. if ((!status) && restart_func) {
  601. restart_func = 0;
  602. goto restart_all;
  603. }
  604. brelse(bh);
  605. bh = NULL;
  606. return status;
  607. }
  608. int ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  609. u32 clusters_to_add, int mark_unwritten)
  610. {
  611. return __ocfs2_extend_allocation(inode, logical_start,
  612. clusters_to_add, mark_unwritten);
  613. }
  614. /*
  615. * While a write will already be ordering the data, a truncate will not.
  616. * Thus, we need to explicitly order the zeroed pages.
  617. */
  618. static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode,
  619. struct buffer_head *di_bh)
  620. {
  621. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  622. handle_t *handle = NULL;
  623. int ret = 0;
  624. if (!ocfs2_should_order_data(inode))
  625. goto out;
  626. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  627. if (IS_ERR(handle)) {
  628. ret = -ENOMEM;
  629. mlog_errno(ret);
  630. goto out;
  631. }
  632. ret = ocfs2_jbd2_file_inode(handle, inode);
  633. if (ret < 0) {
  634. mlog_errno(ret);
  635. goto out;
  636. }
  637. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  638. OCFS2_JOURNAL_ACCESS_WRITE);
  639. if (ret)
  640. mlog_errno(ret);
  641. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  642. out:
  643. if (ret) {
  644. if (!IS_ERR(handle))
  645. ocfs2_commit_trans(osb, handle);
  646. handle = ERR_PTR(ret);
  647. }
  648. return handle;
  649. }
  650. /* Some parts of this taken from generic_cont_expand, which turned out
  651. * to be too fragile to do exactly what we need without us having to
  652. * worry about recursive locking in ->write_begin() and ->write_end(). */
  653. static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
  654. u64 abs_to, struct buffer_head *di_bh)
  655. {
  656. struct address_space *mapping = inode->i_mapping;
  657. struct page *page;
  658. unsigned long index = abs_from >> PAGE_SHIFT;
  659. handle_t *handle;
  660. int ret = 0;
  661. unsigned zero_from, zero_to, block_start, block_end;
  662. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  663. BUG_ON(abs_from >= abs_to);
  664. BUG_ON(abs_to > (((u64)index + 1) << PAGE_SHIFT));
  665. BUG_ON(abs_from & (inode->i_blkbits - 1));
  666. handle = ocfs2_zero_start_ordered_transaction(inode, di_bh);
  667. if (IS_ERR(handle)) {
  668. ret = PTR_ERR(handle);
  669. goto out;
  670. }
  671. page = find_or_create_page(mapping, index, GFP_NOFS);
  672. if (!page) {
  673. ret = -ENOMEM;
  674. mlog_errno(ret);
  675. goto out_commit_trans;
  676. }
  677. /* Get the offsets within the page that we want to zero */
  678. zero_from = abs_from & (PAGE_SIZE - 1);
  679. zero_to = abs_to & (PAGE_SIZE - 1);
  680. if (!zero_to)
  681. zero_to = PAGE_SIZE;
  682. trace_ocfs2_write_zero_page(
  683. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  684. (unsigned long long)abs_from,
  685. (unsigned long long)abs_to,
  686. index, zero_from, zero_to);
  687. /* We know that zero_from is block aligned */
  688. for (block_start = zero_from; block_start < zero_to;
  689. block_start = block_end) {
  690. block_end = block_start + i_blocksize(inode);
  691. /*
  692. * block_start is block-aligned. Bump it by one to force
  693. * __block_write_begin and block_commit_write to zero the
  694. * whole block.
  695. */
  696. ret = __block_write_begin(page, block_start + 1, 0,
  697. ocfs2_get_block);
  698. if (ret < 0) {
  699. mlog_errno(ret);
  700. goto out_unlock;
  701. }
  702. /* must not update i_size! */
  703. ret = block_commit_write(page, block_start + 1,
  704. block_start + 1);
  705. if (ret < 0)
  706. mlog_errno(ret);
  707. else
  708. ret = 0;
  709. }
  710. /*
  711. * fs-writeback will release the dirty pages without page lock
  712. * whose offset are over inode size, the release happens at
  713. * block_write_full_page().
  714. */
  715. i_size_write(inode, abs_to);
  716. inode->i_blocks = ocfs2_inode_sector_count(inode);
  717. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  718. inode->i_mtime = inode->i_ctime = current_time(inode);
  719. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  720. di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  721. di->i_mtime_nsec = di->i_ctime_nsec;
  722. if (handle) {
  723. ocfs2_journal_dirty(handle, di_bh);
  724. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  725. }
  726. out_unlock:
  727. unlock_page(page);
  728. put_page(page);
  729. out_commit_trans:
  730. if (handle)
  731. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  732. out:
  733. return ret;
  734. }
  735. /*
  736. * Find the next range to zero. We do this in terms of bytes because
  737. * that's what ocfs2_zero_extend() wants, and it is dealing with the
  738. * pagecache. We may return multiple extents.
  739. *
  740. * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
  741. * needs to be zeroed. range_start and range_end return the next zeroing
  742. * range. A subsequent call should pass the previous range_end as its
  743. * zero_start. If range_end is 0, there's nothing to do.
  744. *
  745. * Unwritten extents are skipped over. Refcounted extents are CoWd.
  746. */
  747. static int ocfs2_zero_extend_get_range(struct inode *inode,
  748. struct buffer_head *di_bh,
  749. u64 zero_start, u64 zero_end,
  750. u64 *range_start, u64 *range_end)
  751. {
  752. int rc = 0, needs_cow = 0;
  753. u32 p_cpos, zero_clusters = 0;
  754. u32 zero_cpos =
  755. zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  756. u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
  757. unsigned int num_clusters = 0;
  758. unsigned int ext_flags = 0;
  759. while (zero_cpos < last_cpos) {
  760. rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
  761. &num_clusters, &ext_flags);
  762. if (rc) {
  763. mlog_errno(rc);
  764. goto out;
  765. }
  766. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  767. zero_clusters = num_clusters;
  768. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  769. needs_cow = 1;
  770. break;
  771. }
  772. zero_cpos += num_clusters;
  773. }
  774. if (!zero_clusters) {
  775. *range_end = 0;
  776. goto out;
  777. }
  778. while ((zero_cpos + zero_clusters) < last_cpos) {
  779. rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
  780. &p_cpos, &num_clusters,
  781. &ext_flags);
  782. if (rc) {
  783. mlog_errno(rc);
  784. goto out;
  785. }
  786. if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
  787. break;
  788. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  789. needs_cow = 1;
  790. zero_clusters += num_clusters;
  791. }
  792. if ((zero_cpos + zero_clusters) > last_cpos)
  793. zero_clusters = last_cpos - zero_cpos;
  794. if (needs_cow) {
  795. rc = ocfs2_refcount_cow(inode, di_bh, zero_cpos,
  796. zero_clusters, UINT_MAX);
  797. if (rc) {
  798. mlog_errno(rc);
  799. goto out;
  800. }
  801. }
  802. *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
  803. *range_end = ocfs2_clusters_to_bytes(inode->i_sb,
  804. zero_cpos + zero_clusters);
  805. out:
  806. return rc;
  807. }
  808. /*
  809. * Zero one range returned from ocfs2_zero_extend_get_range(). The caller
  810. * has made sure that the entire range needs zeroing.
  811. */
  812. static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
  813. u64 range_end, struct buffer_head *di_bh)
  814. {
  815. int rc = 0;
  816. u64 next_pos;
  817. u64 zero_pos = range_start;
  818. trace_ocfs2_zero_extend_range(
  819. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  820. (unsigned long long)range_start,
  821. (unsigned long long)range_end);
  822. BUG_ON(range_start >= range_end);
  823. while (zero_pos < range_end) {
  824. next_pos = (zero_pos & PAGE_MASK) + PAGE_SIZE;
  825. if (next_pos > range_end)
  826. next_pos = range_end;
  827. rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh);
  828. if (rc < 0) {
  829. mlog_errno(rc);
  830. break;
  831. }
  832. zero_pos = next_pos;
  833. /*
  834. * Very large extends have the potential to lock up
  835. * the cpu for extended periods of time.
  836. */
  837. cond_resched();
  838. }
  839. return rc;
  840. }
  841. int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
  842. loff_t zero_to_size)
  843. {
  844. int ret = 0;
  845. u64 zero_start, range_start = 0, range_end = 0;
  846. struct super_block *sb = inode->i_sb;
  847. zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  848. trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
  849. (unsigned long long)zero_start,
  850. (unsigned long long)i_size_read(inode));
  851. while (zero_start < zero_to_size) {
  852. ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
  853. zero_to_size,
  854. &range_start,
  855. &range_end);
  856. if (ret) {
  857. mlog_errno(ret);
  858. break;
  859. }
  860. if (!range_end)
  861. break;
  862. /* Trim the ends */
  863. if (range_start < zero_start)
  864. range_start = zero_start;
  865. if (range_end > zero_to_size)
  866. range_end = zero_to_size;
  867. ret = ocfs2_zero_extend_range(inode, range_start,
  868. range_end, di_bh);
  869. if (ret) {
  870. mlog_errno(ret);
  871. break;
  872. }
  873. zero_start = range_end;
  874. }
  875. return ret;
  876. }
  877. int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
  878. u64 new_i_size, u64 zero_to)
  879. {
  880. int ret;
  881. u32 clusters_to_add;
  882. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  883. /*
  884. * Only quota files call this without a bh, and they can't be
  885. * refcounted.
  886. */
  887. BUG_ON(!di_bh && ocfs2_is_refcount_inode(inode));
  888. BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
  889. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  890. if (clusters_to_add < oi->ip_clusters)
  891. clusters_to_add = 0;
  892. else
  893. clusters_to_add -= oi->ip_clusters;
  894. if (clusters_to_add) {
  895. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  896. clusters_to_add, 0);
  897. if (ret) {
  898. mlog_errno(ret);
  899. goto out;
  900. }
  901. }
  902. /*
  903. * Call this even if we don't add any clusters to the tree. We
  904. * still need to zero the area between the old i_size and the
  905. * new i_size.
  906. */
  907. ret = ocfs2_zero_extend(inode, di_bh, zero_to);
  908. if (ret < 0)
  909. mlog_errno(ret);
  910. out:
  911. return ret;
  912. }
  913. static int ocfs2_extend_file(struct inode *inode,
  914. struct buffer_head *di_bh,
  915. u64 new_i_size)
  916. {
  917. int ret = 0;
  918. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  919. BUG_ON(!di_bh);
  920. /* setattr sometimes calls us like this. */
  921. if (new_i_size == 0)
  922. goto out;
  923. if (i_size_read(inode) == new_i_size)
  924. goto out;
  925. BUG_ON(new_i_size < i_size_read(inode));
  926. /*
  927. * The alloc sem blocks people in read/write from reading our
  928. * allocation until we're done changing it. We depend on
  929. * i_mutex to block other extend/truncate calls while we're
  930. * here. We even have to hold it for sparse files because there
  931. * might be some tail zeroing.
  932. */
  933. down_write(&oi->ip_alloc_sem);
  934. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  935. /*
  936. * We can optimize small extends by keeping the inodes
  937. * inline data.
  938. */
  939. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  940. up_write(&oi->ip_alloc_sem);
  941. goto out_update_size;
  942. }
  943. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  944. if (ret) {
  945. up_write(&oi->ip_alloc_sem);
  946. mlog_errno(ret);
  947. goto out;
  948. }
  949. }
  950. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  951. ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
  952. else
  953. ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
  954. new_i_size);
  955. up_write(&oi->ip_alloc_sem);
  956. if (ret < 0) {
  957. mlog_errno(ret);
  958. goto out;
  959. }
  960. out_update_size:
  961. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  962. if (ret < 0)
  963. mlog_errno(ret);
  964. out:
  965. return ret;
  966. }
  967. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  968. {
  969. int status = 0, size_change;
  970. int inode_locked = 0;
  971. struct inode *inode = d_inode(dentry);
  972. struct super_block *sb = inode->i_sb;
  973. struct ocfs2_super *osb = OCFS2_SB(sb);
  974. struct buffer_head *bh = NULL;
  975. handle_t *handle = NULL;
  976. struct dquot *transfer_to[MAXQUOTAS] = { };
  977. int qtype;
  978. int had_lock;
  979. struct ocfs2_lock_holder oh;
  980. trace_ocfs2_setattr(inode, dentry,
  981. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  982. dentry->d_name.len, dentry->d_name.name,
  983. attr->ia_valid, attr->ia_mode,
  984. from_kuid(&init_user_ns, attr->ia_uid),
  985. from_kgid(&init_user_ns, attr->ia_gid));
  986. /* ensuring we don't even attempt to truncate a symlink */
  987. if (S_ISLNK(inode->i_mode))
  988. attr->ia_valid &= ~ATTR_SIZE;
  989. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  990. | ATTR_GID | ATTR_UID | ATTR_MODE)
  991. if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
  992. return 0;
  993. status = setattr_prepare(dentry, attr);
  994. if (status)
  995. return status;
  996. if (is_quota_modification(inode, attr)) {
  997. status = dquot_initialize(inode);
  998. if (status)
  999. return status;
  1000. }
  1001. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  1002. if (size_change) {
  1003. status = ocfs2_rw_lock(inode, 1);
  1004. if (status < 0) {
  1005. mlog_errno(status);
  1006. goto bail;
  1007. }
  1008. }
  1009. had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
  1010. if (had_lock < 0) {
  1011. status = had_lock;
  1012. goto bail_unlock_rw;
  1013. } else if (had_lock) {
  1014. /*
  1015. * As far as we know, ocfs2_setattr() could only be the first
  1016. * VFS entry point in the call chain of recursive cluster
  1017. * locking issue.
  1018. *
  1019. * For instance:
  1020. * chmod_common()
  1021. * notify_change()
  1022. * ocfs2_setattr()
  1023. * posix_acl_chmod()
  1024. * ocfs2_iop_get_acl()
  1025. *
  1026. * But, we're not 100% sure if it's always true, because the
  1027. * ordering of the VFS entry points in the call chain is out
  1028. * of our control. So, we'd better dump the stack here to
  1029. * catch the other cases of recursive locking.
  1030. */
  1031. mlog(ML_ERROR, "Another case of recursive locking:\n");
  1032. dump_stack();
  1033. }
  1034. inode_locked = 1;
  1035. if (size_change) {
  1036. status = inode_newsize_ok(inode, attr->ia_size);
  1037. if (status)
  1038. goto bail_unlock;
  1039. inode_dio_wait(inode);
  1040. if (i_size_read(inode) >= attr->ia_size) {
  1041. if (ocfs2_should_order_data(inode)) {
  1042. status = ocfs2_begin_ordered_truncate(inode,
  1043. attr->ia_size);
  1044. if (status)
  1045. goto bail_unlock;
  1046. }
  1047. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  1048. } else
  1049. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  1050. if (status < 0) {
  1051. if (status != -ENOSPC)
  1052. mlog_errno(status);
  1053. status = -ENOSPC;
  1054. goto bail_unlock;
  1055. }
  1056. }
  1057. if ((attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  1058. (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  1059. /*
  1060. * Gather pointers to quota structures so that allocation /
  1061. * freeing of quota structures happens here and not inside
  1062. * dquot_transfer() where we have problems with lock ordering
  1063. */
  1064. if (attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)
  1065. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1066. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  1067. transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid));
  1068. if (IS_ERR(transfer_to[USRQUOTA])) {
  1069. status = PTR_ERR(transfer_to[USRQUOTA]);
  1070. goto bail_unlock;
  1071. }
  1072. }
  1073. if (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid)
  1074. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1075. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  1076. transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid));
  1077. if (IS_ERR(transfer_to[GRPQUOTA])) {
  1078. status = PTR_ERR(transfer_to[GRPQUOTA]);
  1079. goto bail_unlock;
  1080. }
  1081. }
  1082. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
  1083. 2 * ocfs2_quota_trans_credits(sb));
  1084. if (IS_ERR(handle)) {
  1085. status = PTR_ERR(handle);
  1086. mlog_errno(status);
  1087. goto bail_unlock;
  1088. }
  1089. status = __dquot_transfer(inode, transfer_to);
  1090. if (status < 0)
  1091. goto bail_commit;
  1092. } else {
  1093. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1094. if (IS_ERR(handle)) {
  1095. status = PTR_ERR(handle);
  1096. mlog_errno(status);
  1097. goto bail_unlock;
  1098. }
  1099. }
  1100. setattr_copy(inode, attr);
  1101. mark_inode_dirty(inode);
  1102. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  1103. if (status < 0)
  1104. mlog_errno(status);
  1105. bail_commit:
  1106. ocfs2_commit_trans(osb, handle);
  1107. bail_unlock:
  1108. if (status && inode_locked) {
  1109. ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
  1110. inode_locked = 0;
  1111. }
  1112. bail_unlock_rw:
  1113. if (size_change)
  1114. ocfs2_rw_unlock(inode, 1);
  1115. bail:
  1116. /* Release quota pointers in case we acquired them */
  1117. for (qtype = 0; qtype < OCFS2_MAXQUOTAS; qtype++)
  1118. dqput(transfer_to[qtype]);
  1119. if (!status && attr->ia_valid & ATTR_MODE) {
  1120. status = ocfs2_acl_chmod(inode, bh);
  1121. if (status < 0)
  1122. mlog_errno(status);
  1123. }
  1124. if (inode_locked)
  1125. ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
  1126. brelse(bh);
  1127. return status;
  1128. }
  1129. int ocfs2_getattr(const struct path *path, struct kstat *stat,
  1130. u32 request_mask, unsigned int flags)
  1131. {
  1132. struct inode *inode = d_inode(path->dentry);
  1133. struct super_block *sb = path->dentry->d_sb;
  1134. struct ocfs2_super *osb = sb->s_fs_info;
  1135. int err;
  1136. err = ocfs2_inode_revalidate(path->dentry);
  1137. if (err) {
  1138. if (err != -ENOENT)
  1139. mlog_errno(err);
  1140. goto bail;
  1141. }
  1142. generic_fillattr(inode, stat);
  1143. /*
  1144. * If there is inline data in the inode, the inode will normally not
  1145. * have data blocks allocated (it may have an external xattr block).
  1146. * Report at least one sector for such files, so tools like tar, rsync,
  1147. * others don't incorrectly think the file is completely sparse.
  1148. */
  1149. if (unlikely(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  1150. stat->blocks += (stat->size + 511)>>9;
  1151. /* We set the blksize from the cluster size for performance */
  1152. stat->blksize = osb->s_clustersize;
  1153. bail:
  1154. return err;
  1155. }
  1156. int ocfs2_permission(struct inode *inode, int mask)
  1157. {
  1158. int ret, had_lock;
  1159. struct ocfs2_lock_holder oh;
  1160. if (mask & MAY_NOT_BLOCK)
  1161. return -ECHILD;
  1162. had_lock = ocfs2_inode_lock_tracker(inode, NULL, 0, &oh);
  1163. if (had_lock < 0) {
  1164. ret = had_lock;
  1165. goto out;
  1166. } else if (had_lock) {
  1167. /* See comments in ocfs2_setattr() for details.
  1168. * The call chain of this case could be:
  1169. * do_sys_open()
  1170. * may_open()
  1171. * inode_permission()
  1172. * ocfs2_permission()
  1173. * ocfs2_iop_get_acl()
  1174. */
  1175. mlog(ML_ERROR, "Another case of recursive locking:\n");
  1176. dump_stack();
  1177. }
  1178. ret = generic_permission(inode, mask);
  1179. ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
  1180. out:
  1181. return ret;
  1182. }
  1183. static int __ocfs2_write_remove_suid(struct inode *inode,
  1184. struct buffer_head *bh)
  1185. {
  1186. int ret;
  1187. handle_t *handle;
  1188. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1189. struct ocfs2_dinode *di;
  1190. trace_ocfs2_write_remove_suid(
  1191. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1192. inode->i_mode);
  1193. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1194. if (IS_ERR(handle)) {
  1195. ret = PTR_ERR(handle);
  1196. mlog_errno(ret);
  1197. goto out;
  1198. }
  1199. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1200. OCFS2_JOURNAL_ACCESS_WRITE);
  1201. if (ret < 0) {
  1202. mlog_errno(ret);
  1203. goto out_trans;
  1204. }
  1205. inode->i_mode &= ~S_ISUID;
  1206. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  1207. inode->i_mode &= ~S_ISGID;
  1208. di = (struct ocfs2_dinode *) bh->b_data;
  1209. di->i_mode = cpu_to_le16(inode->i_mode);
  1210. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  1211. ocfs2_journal_dirty(handle, bh);
  1212. out_trans:
  1213. ocfs2_commit_trans(osb, handle);
  1214. out:
  1215. return ret;
  1216. }
  1217. static int ocfs2_write_remove_suid(struct inode *inode)
  1218. {
  1219. int ret;
  1220. struct buffer_head *bh = NULL;
  1221. ret = ocfs2_read_inode_block(inode, &bh);
  1222. if (ret < 0) {
  1223. mlog_errno(ret);
  1224. goto out;
  1225. }
  1226. ret = __ocfs2_write_remove_suid(inode, bh);
  1227. out:
  1228. brelse(bh);
  1229. return ret;
  1230. }
  1231. /*
  1232. * Allocate enough extents to cover the region starting at byte offset
  1233. * start for len bytes. Existing extents are skipped, any extents
  1234. * added are marked as "unwritten".
  1235. */
  1236. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1237. u64 start, u64 len)
  1238. {
  1239. int ret;
  1240. u32 cpos, phys_cpos, clusters, alloc_size;
  1241. u64 end = start + len;
  1242. struct buffer_head *di_bh = NULL;
  1243. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1244. ret = ocfs2_read_inode_block(inode, &di_bh);
  1245. if (ret) {
  1246. mlog_errno(ret);
  1247. goto out;
  1248. }
  1249. /*
  1250. * Nothing to do if the requested reservation range
  1251. * fits within the inode.
  1252. */
  1253. if (ocfs2_size_fits_inline_data(di_bh, end))
  1254. goto out;
  1255. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  1256. if (ret) {
  1257. mlog_errno(ret);
  1258. goto out;
  1259. }
  1260. }
  1261. /*
  1262. * We consider both start and len to be inclusive.
  1263. */
  1264. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1265. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1266. clusters -= cpos;
  1267. while (clusters) {
  1268. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1269. &alloc_size, NULL);
  1270. if (ret) {
  1271. mlog_errno(ret);
  1272. goto out;
  1273. }
  1274. /*
  1275. * Hole or existing extent len can be arbitrary, so
  1276. * cap it to our own allocation request.
  1277. */
  1278. if (alloc_size > clusters)
  1279. alloc_size = clusters;
  1280. if (phys_cpos) {
  1281. /*
  1282. * We already have an allocation at this
  1283. * region so we can safely skip it.
  1284. */
  1285. goto next;
  1286. }
  1287. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1288. if (ret) {
  1289. if (ret != -ENOSPC)
  1290. mlog_errno(ret);
  1291. goto out;
  1292. }
  1293. next:
  1294. cpos += alloc_size;
  1295. clusters -= alloc_size;
  1296. }
  1297. ret = 0;
  1298. out:
  1299. brelse(di_bh);
  1300. return ret;
  1301. }
  1302. /*
  1303. * Truncate a byte range, avoiding pages within partial clusters. This
  1304. * preserves those pages for the zeroing code to write to.
  1305. */
  1306. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1307. u64 byte_len)
  1308. {
  1309. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1310. loff_t start, end;
  1311. struct address_space *mapping = inode->i_mapping;
  1312. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1313. end = byte_start + byte_len;
  1314. end = end & ~(osb->s_clustersize - 1);
  1315. if (start < end) {
  1316. unmap_mapping_range(mapping, start, end - start, 0);
  1317. truncate_inode_pages_range(mapping, start, end - 1);
  1318. }
  1319. }
  1320. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1321. u64 start, u64 len)
  1322. {
  1323. int ret = 0;
  1324. u64 tmpend = 0;
  1325. u64 end = start + len;
  1326. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1327. unsigned int csize = osb->s_clustersize;
  1328. handle_t *handle;
  1329. /*
  1330. * The "start" and "end" values are NOT necessarily part of
  1331. * the range whose allocation is being deleted. Rather, this
  1332. * is what the user passed in with the request. We must zero
  1333. * partial clusters here. There's no need to worry about
  1334. * physical allocation - the zeroing code knows to skip holes.
  1335. */
  1336. trace_ocfs2_zero_partial_clusters(
  1337. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1338. (unsigned long long)start, (unsigned long long)end);
  1339. /*
  1340. * If both edges are on a cluster boundary then there's no
  1341. * zeroing required as the region is part of the allocation to
  1342. * be truncated.
  1343. */
  1344. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1345. goto out;
  1346. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1347. if (IS_ERR(handle)) {
  1348. ret = PTR_ERR(handle);
  1349. mlog_errno(ret);
  1350. goto out;
  1351. }
  1352. /*
  1353. * If start is on a cluster boundary and end is somewhere in another
  1354. * cluster, we have not COWed the cluster starting at start, unless
  1355. * end is also within the same cluster. So, in this case, we skip this
  1356. * first call to ocfs2_zero_range_for_truncate() truncate and move on
  1357. * to the next one.
  1358. */
  1359. if ((start & (csize - 1)) != 0) {
  1360. /*
  1361. * We want to get the byte offset of the end of the 1st
  1362. * cluster.
  1363. */
  1364. tmpend = (u64)osb->s_clustersize +
  1365. (start & ~(osb->s_clustersize - 1));
  1366. if (tmpend > end)
  1367. tmpend = end;
  1368. trace_ocfs2_zero_partial_clusters_range1(
  1369. (unsigned long long)start,
  1370. (unsigned long long)tmpend);
  1371. ret = ocfs2_zero_range_for_truncate(inode, handle, start,
  1372. tmpend);
  1373. if (ret)
  1374. mlog_errno(ret);
  1375. }
  1376. if (tmpend < end) {
  1377. /*
  1378. * This may make start and end equal, but the zeroing
  1379. * code will skip any work in that case so there's no
  1380. * need to catch it up here.
  1381. */
  1382. start = end & ~(osb->s_clustersize - 1);
  1383. trace_ocfs2_zero_partial_clusters_range2(
  1384. (unsigned long long)start, (unsigned long long)end);
  1385. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1386. if (ret)
  1387. mlog_errno(ret);
  1388. }
  1389. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  1390. ocfs2_commit_trans(osb, handle);
  1391. out:
  1392. return ret;
  1393. }
  1394. static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
  1395. {
  1396. int i;
  1397. struct ocfs2_extent_rec *rec = NULL;
  1398. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1399. rec = &el->l_recs[i];
  1400. if (le32_to_cpu(rec->e_cpos) < pos)
  1401. break;
  1402. }
  1403. return i;
  1404. }
  1405. /*
  1406. * Helper to calculate the punching pos and length in one run, we handle the
  1407. * following three cases in order:
  1408. *
  1409. * - remove the entire record
  1410. * - remove a partial record
  1411. * - no record needs to be removed (hole-punching completed)
  1412. */
  1413. static void ocfs2_calc_trunc_pos(struct inode *inode,
  1414. struct ocfs2_extent_list *el,
  1415. struct ocfs2_extent_rec *rec,
  1416. u32 trunc_start, u32 *trunc_cpos,
  1417. u32 *trunc_len, u32 *trunc_end,
  1418. u64 *blkno, int *done)
  1419. {
  1420. int ret = 0;
  1421. u32 coff, range;
  1422. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1423. if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
  1424. /*
  1425. * remove an entire extent record.
  1426. */
  1427. *trunc_cpos = le32_to_cpu(rec->e_cpos);
  1428. /*
  1429. * Skip holes if any.
  1430. */
  1431. if (range < *trunc_end)
  1432. *trunc_end = range;
  1433. *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
  1434. *blkno = le64_to_cpu(rec->e_blkno);
  1435. *trunc_end = le32_to_cpu(rec->e_cpos);
  1436. } else if (range > trunc_start) {
  1437. /*
  1438. * remove a partial extent record, which means we're
  1439. * removing the last extent record.
  1440. */
  1441. *trunc_cpos = trunc_start;
  1442. /*
  1443. * skip hole if any.
  1444. */
  1445. if (range < *trunc_end)
  1446. *trunc_end = range;
  1447. *trunc_len = *trunc_end - trunc_start;
  1448. coff = trunc_start - le32_to_cpu(rec->e_cpos);
  1449. *blkno = le64_to_cpu(rec->e_blkno) +
  1450. ocfs2_clusters_to_blocks(inode->i_sb, coff);
  1451. *trunc_end = trunc_start;
  1452. } else {
  1453. /*
  1454. * It may have two following possibilities:
  1455. *
  1456. * - last record has been removed
  1457. * - trunc_start was within a hole
  1458. *
  1459. * both two cases mean the completion of hole punching.
  1460. */
  1461. ret = 1;
  1462. }
  1463. *done = ret;
  1464. }
  1465. int ocfs2_remove_inode_range(struct inode *inode,
  1466. struct buffer_head *di_bh, u64 byte_start,
  1467. u64 byte_len)
  1468. {
  1469. int ret = 0, flags = 0, done = 0, i;
  1470. u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
  1471. u32 cluster_in_el;
  1472. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1473. struct ocfs2_cached_dealloc_ctxt dealloc;
  1474. struct address_space *mapping = inode->i_mapping;
  1475. struct ocfs2_extent_tree et;
  1476. struct ocfs2_path *path = NULL;
  1477. struct ocfs2_extent_list *el = NULL;
  1478. struct ocfs2_extent_rec *rec = NULL;
  1479. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1480. u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
  1481. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1482. ocfs2_init_dealloc_ctxt(&dealloc);
  1483. trace_ocfs2_remove_inode_range(
  1484. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1485. (unsigned long long)byte_start,
  1486. (unsigned long long)byte_len);
  1487. if (byte_len == 0)
  1488. return 0;
  1489. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1490. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1491. byte_start + byte_len, 0);
  1492. if (ret) {
  1493. mlog_errno(ret);
  1494. goto out;
  1495. }
  1496. /*
  1497. * There's no need to get fancy with the page cache
  1498. * truncate of an inline-data inode. We're talking
  1499. * about less than a page here, which will be cached
  1500. * in the dinode buffer anyway.
  1501. */
  1502. unmap_mapping_range(mapping, 0, 0, 0);
  1503. truncate_inode_pages(mapping, 0);
  1504. goto out;
  1505. }
  1506. /*
  1507. * For reflinks, we may need to CoW 2 clusters which might be
  1508. * partially zero'd later, if hole's start and end offset were
  1509. * within one cluster(means is not exactly aligned to clustersize).
  1510. */
  1511. if (ocfs2_is_refcount_inode(inode)) {
  1512. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
  1513. if (ret) {
  1514. mlog_errno(ret);
  1515. goto out;
  1516. }
  1517. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
  1518. if (ret) {
  1519. mlog_errno(ret);
  1520. goto out;
  1521. }
  1522. }
  1523. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1524. trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1525. cluster_in_el = trunc_end;
  1526. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1527. if (ret) {
  1528. mlog_errno(ret);
  1529. goto out;
  1530. }
  1531. path = ocfs2_new_path_from_et(&et);
  1532. if (!path) {
  1533. ret = -ENOMEM;
  1534. mlog_errno(ret);
  1535. goto out;
  1536. }
  1537. while (trunc_end > trunc_start) {
  1538. ret = ocfs2_find_path(INODE_CACHE(inode), path,
  1539. cluster_in_el);
  1540. if (ret) {
  1541. mlog_errno(ret);
  1542. goto out;
  1543. }
  1544. el = path_leaf_el(path);
  1545. i = ocfs2_find_rec(el, trunc_end);
  1546. /*
  1547. * Need to go to previous extent block.
  1548. */
  1549. if (i < 0) {
  1550. if (path->p_tree_depth == 0)
  1551. break;
  1552. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
  1553. path,
  1554. &cluster_in_el);
  1555. if (ret) {
  1556. mlog_errno(ret);
  1557. goto out;
  1558. }
  1559. /*
  1560. * We've reached the leftmost extent block,
  1561. * it's safe to leave.
  1562. */
  1563. if (cluster_in_el == 0)
  1564. break;
  1565. /*
  1566. * The 'pos' searched for previous extent block is
  1567. * always one cluster less than actual trunc_end.
  1568. */
  1569. trunc_end = cluster_in_el + 1;
  1570. ocfs2_reinit_path(path, 1);
  1571. continue;
  1572. } else
  1573. rec = &el->l_recs[i];
  1574. ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
  1575. &trunc_len, &trunc_end, &blkno, &done);
  1576. if (done)
  1577. break;
  1578. flags = rec->e_flags;
  1579. phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  1580. ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
  1581. phys_cpos, trunc_len, flags,
  1582. &dealloc, refcount_loc, false);
  1583. if (ret < 0) {
  1584. mlog_errno(ret);
  1585. goto out;
  1586. }
  1587. cluster_in_el = trunc_end;
  1588. ocfs2_reinit_path(path, 1);
  1589. }
  1590. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1591. out:
  1592. ocfs2_free_path(path);
  1593. ocfs2_schedule_truncate_log_flush(osb, 1);
  1594. ocfs2_run_deallocs(osb, &dealloc);
  1595. return ret;
  1596. }
  1597. /*
  1598. * Parts of this function taken from xfs_change_file_space()
  1599. */
  1600. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1601. loff_t f_pos, unsigned int cmd,
  1602. struct ocfs2_space_resv *sr,
  1603. int change_size)
  1604. {
  1605. int ret;
  1606. s64 llen;
  1607. loff_t size;
  1608. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1609. struct buffer_head *di_bh = NULL;
  1610. handle_t *handle;
  1611. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1612. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1613. return -EROFS;
  1614. inode_lock(inode);
  1615. /*
  1616. * This prevents concurrent writes on other nodes
  1617. */
  1618. ret = ocfs2_rw_lock(inode, 1);
  1619. if (ret) {
  1620. mlog_errno(ret);
  1621. goto out;
  1622. }
  1623. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1624. if (ret) {
  1625. mlog_errno(ret);
  1626. goto out_rw_unlock;
  1627. }
  1628. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1629. ret = -EPERM;
  1630. goto out_inode_unlock;
  1631. }
  1632. switch (sr->l_whence) {
  1633. case 0: /*SEEK_SET*/
  1634. break;
  1635. case 1: /*SEEK_CUR*/
  1636. sr->l_start += f_pos;
  1637. break;
  1638. case 2: /*SEEK_END*/
  1639. sr->l_start += i_size_read(inode);
  1640. break;
  1641. default:
  1642. ret = -EINVAL;
  1643. goto out_inode_unlock;
  1644. }
  1645. sr->l_whence = 0;
  1646. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1647. if (sr->l_start < 0
  1648. || sr->l_start > max_off
  1649. || (sr->l_start + llen) < 0
  1650. || (sr->l_start + llen) > max_off) {
  1651. ret = -EINVAL;
  1652. goto out_inode_unlock;
  1653. }
  1654. size = sr->l_start + sr->l_len;
  1655. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64 ||
  1656. cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) {
  1657. if (sr->l_len <= 0) {
  1658. ret = -EINVAL;
  1659. goto out_inode_unlock;
  1660. }
  1661. }
  1662. if (file && should_remove_suid(file->f_path.dentry)) {
  1663. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1664. if (ret) {
  1665. mlog_errno(ret);
  1666. goto out_inode_unlock;
  1667. }
  1668. }
  1669. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1670. switch (cmd) {
  1671. case OCFS2_IOC_RESVSP:
  1672. case OCFS2_IOC_RESVSP64:
  1673. /*
  1674. * This takes unsigned offsets, but the signed ones we
  1675. * pass have been checked against overflow above.
  1676. */
  1677. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1678. sr->l_len);
  1679. break;
  1680. case OCFS2_IOC_UNRESVSP:
  1681. case OCFS2_IOC_UNRESVSP64:
  1682. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1683. sr->l_len);
  1684. break;
  1685. default:
  1686. ret = -EINVAL;
  1687. }
  1688. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1689. if (ret) {
  1690. mlog_errno(ret);
  1691. goto out_inode_unlock;
  1692. }
  1693. /*
  1694. * We update c/mtime for these changes
  1695. */
  1696. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1697. if (IS_ERR(handle)) {
  1698. ret = PTR_ERR(handle);
  1699. mlog_errno(ret);
  1700. goto out_inode_unlock;
  1701. }
  1702. if (change_size && i_size_read(inode) < size)
  1703. i_size_write(inode, size);
  1704. inode->i_ctime = inode->i_mtime = current_time(inode);
  1705. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1706. if (ret < 0)
  1707. mlog_errno(ret);
  1708. if (file && (file->f_flags & O_SYNC))
  1709. handle->h_sync = 1;
  1710. ocfs2_commit_trans(osb, handle);
  1711. out_inode_unlock:
  1712. brelse(di_bh);
  1713. ocfs2_inode_unlock(inode, 1);
  1714. out_rw_unlock:
  1715. ocfs2_rw_unlock(inode, 1);
  1716. out:
  1717. inode_unlock(inode);
  1718. return ret;
  1719. }
  1720. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1721. struct ocfs2_space_resv *sr)
  1722. {
  1723. struct inode *inode = file_inode(file);
  1724. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1725. int ret;
  1726. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1727. !ocfs2_writes_unwritten_extents(osb))
  1728. return -ENOTTY;
  1729. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1730. !ocfs2_sparse_alloc(osb))
  1731. return -ENOTTY;
  1732. if (!S_ISREG(inode->i_mode))
  1733. return -EINVAL;
  1734. if (!(file->f_mode & FMODE_WRITE))
  1735. return -EBADF;
  1736. ret = mnt_want_write_file(file);
  1737. if (ret)
  1738. return ret;
  1739. ret = __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1740. mnt_drop_write_file(file);
  1741. return ret;
  1742. }
  1743. static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
  1744. loff_t len)
  1745. {
  1746. struct inode *inode = file_inode(file);
  1747. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1748. struct ocfs2_space_resv sr;
  1749. int change_size = 1;
  1750. int cmd = OCFS2_IOC_RESVSP64;
  1751. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1752. return -EOPNOTSUPP;
  1753. if (!ocfs2_writes_unwritten_extents(osb))
  1754. return -EOPNOTSUPP;
  1755. if (mode & FALLOC_FL_KEEP_SIZE)
  1756. change_size = 0;
  1757. if (mode & FALLOC_FL_PUNCH_HOLE)
  1758. cmd = OCFS2_IOC_UNRESVSP64;
  1759. sr.l_whence = 0;
  1760. sr.l_start = (s64)offset;
  1761. sr.l_len = (s64)len;
  1762. return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
  1763. change_size);
  1764. }
  1765. int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
  1766. size_t count)
  1767. {
  1768. int ret = 0;
  1769. unsigned int extent_flags;
  1770. u32 cpos, clusters, extent_len, phys_cpos;
  1771. struct super_block *sb = inode->i_sb;
  1772. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
  1773. !ocfs2_is_refcount_inode(inode) ||
  1774. OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1775. return 0;
  1776. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1777. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1778. while (clusters) {
  1779. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1780. &extent_flags);
  1781. if (ret < 0) {
  1782. mlog_errno(ret);
  1783. goto out;
  1784. }
  1785. if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
  1786. ret = 1;
  1787. break;
  1788. }
  1789. if (extent_len > clusters)
  1790. extent_len = clusters;
  1791. clusters -= extent_len;
  1792. cpos += extent_len;
  1793. }
  1794. out:
  1795. return ret;
  1796. }
  1797. static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
  1798. {
  1799. int blockmask = inode->i_sb->s_blocksize - 1;
  1800. loff_t final_size = pos + count;
  1801. if ((pos & blockmask) || (final_size & blockmask))
  1802. return 1;
  1803. return 0;
  1804. }
  1805. static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
  1806. struct file *file,
  1807. loff_t pos, size_t count,
  1808. int *meta_level)
  1809. {
  1810. int ret;
  1811. struct buffer_head *di_bh = NULL;
  1812. u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1813. u32 clusters =
  1814. ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
  1815. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1816. if (ret) {
  1817. mlog_errno(ret);
  1818. goto out;
  1819. }
  1820. *meta_level = 1;
  1821. ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
  1822. if (ret)
  1823. mlog_errno(ret);
  1824. out:
  1825. brelse(di_bh);
  1826. return ret;
  1827. }
  1828. static int ocfs2_prepare_inode_for_write(struct file *file,
  1829. loff_t pos,
  1830. size_t count)
  1831. {
  1832. int ret = 0, meta_level = 0;
  1833. struct dentry *dentry = file->f_path.dentry;
  1834. struct inode *inode = d_inode(dentry);
  1835. loff_t end;
  1836. /*
  1837. * We start with a read level meta lock and only jump to an ex
  1838. * if we need to make modifications here.
  1839. */
  1840. for(;;) {
  1841. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1842. if (ret < 0) {
  1843. meta_level = -1;
  1844. mlog_errno(ret);
  1845. goto out;
  1846. }
  1847. /* Clear suid / sgid if necessary. We do this here
  1848. * instead of later in the write path because
  1849. * remove_suid() calls ->setattr without any hint that
  1850. * we may have already done our cluster locking. Since
  1851. * ocfs2_setattr() *must* take cluster locks to
  1852. * proceed, this will lead us to recursively lock the
  1853. * inode. There's also the dinode i_size state which
  1854. * can be lost via setattr during extending writes (we
  1855. * set inode->i_size at the end of a write. */
  1856. if (should_remove_suid(dentry)) {
  1857. if (meta_level == 0) {
  1858. ocfs2_inode_unlock(inode, meta_level);
  1859. meta_level = 1;
  1860. continue;
  1861. }
  1862. ret = ocfs2_write_remove_suid(inode);
  1863. if (ret < 0) {
  1864. mlog_errno(ret);
  1865. goto out_unlock;
  1866. }
  1867. }
  1868. end = pos + count;
  1869. ret = ocfs2_check_range_for_refcount(inode, pos, count);
  1870. if (ret == 1) {
  1871. ocfs2_inode_unlock(inode, meta_level);
  1872. meta_level = -1;
  1873. ret = ocfs2_prepare_inode_for_refcount(inode,
  1874. file,
  1875. pos,
  1876. count,
  1877. &meta_level);
  1878. }
  1879. if (ret < 0) {
  1880. mlog_errno(ret);
  1881. goto out_unlock;
  1882. }
  1883. break;
  1884. }
  1885. out_unlock:
  1886. trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
  1887. pos, count);
  1888. if (meta_level >= 0)
  1889. ocfs2_inode_unlock(inode, meta_level);
  1890. out:
  1891. return ret;
  1892. }
  1893. static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
  1894. struct iov_iter *from)
  1895. {
  1896. int direct_io, rw_level;
  1897. ssize_t written = 0;
  1898. ssize_t ret;
  1899. size_t count = iov_iter_count(from);
  1900. struct file *file = iocb->ki_filp;
  1901. struct inode *inode = file_inode(file);
  1902. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1903. int full_coherency = !(osb->s_mount_opt &
  1904. OCFS2_MOUNT_COHERENCY_BUFFERED);
  1905. void *saved_ki_complete = NULL;
  1906. int append_write = ((iocb->ki_pos + count) >=
  1907. i_size_read(inode) ? 1 : 0);
  1908. trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
  1909. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1910. file->f_path.dentry->d_name.len,
  1911. file->f_path.dentry->d_name.name,
  1912. (unsigned int)from->nr_segs); /* GRRRRR */
  1913. if (count == 0)
  1914. return 0;
  1915. direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0;
  1916. inode_lock(inode);
  1917. /*
  1918. * Concurrent O_DIRECT writes are allowed with
  1919. * mount_option "coherency=buffered".
  1920. * For append write, we must take rw EX.
  1921. */
  1922. rw_level = (!direct_io || full_coherency || append_write);
  1923. ret = ocfs2_rw_lock(inode, rw_level);
  1924. if (ret < 0) {
  1925. mlog_errno(ret);
  1926. goto out_mutex;
  1927. }
  1928. /*
  1929. * O_DIRECT writes with "coherency=full" need to take EX cluster
  1930. * inode_lock to guarantee coherency.
  1931. */
  1932. if (direct_io && full_coherency) {
  1933. /*
  1934. * We need to take and drop the inode lock to force
  1935. * other nodes to drop their caches. Buffered I/O
  1936. * already does this in write_begin().
  1937. */
  1938. ret = ocfs2_inode_lock(inode, NULL, 1);
  1939. if (ret < 0) {
  1940. mlog_errno(ret);
  1941. goto out;
  1942. }
  1943. ocfs2_inode_unlock(inode, 1);
  1944. }
  1945. ret = generic_write_checks(iocb, from);
  1946. if (ret <= 0) {
  1947. if (ret)
  1948. mlog_errno(ret);
  1949. goto out;
  1950. }
  1951. count = ret;
  1952. ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, count);
  1953. if (ret < 0) {
  1954. mlog_errno(ret);
  1955. goto out;
  1956. }
  1957. if (direct_io && !is_sync_kiocb(iocb) &&
  1958. ocfs2_is_io_unaligned(inode, count, iocb->ki_pos)) {
  1959. /*
  1960. * Make it a sync io if it's an unaligned aio.
  1961. */
  1962. saved_ki_complete = xchg(&iocb->ki_complete, NULL);
  1963. }
  1964. /* communicate with ocfs2_dio_end_io */
  1965. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1966. written = __generic_file_write_iter(iocb, from);
  1967. /* buffered aio wouldn't have proper lock coverage today */
  1968. BUG_ON(written == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
  1969. /*
  1970. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1971. * function pointer which is called when o_direct io completes so that
  1972. * it can unlock our rw lock.
  1973. * Unfortunately there are error cases which call end_io and others
  1974. * that don't. so we don't have to unlock the rw_lock if either an
  1975. * async dio is going to do it in the future or an end_io after an
  1976. * error has already done it.
  1977. */
  1978. if ((written == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
  1979. rw_level = -1;
  1980. }
  1981. if (unlikely(written <= 0))
  1982. goto out;
  1983. if (((file->f_flags & O_DSYNC) && !direct_io) ||
  1984. IS_SYNC(inode)) {
  1985. ret = filemap_fdatawrite_range(file->f_mapping,
  1986. iocb->ki_pos - written,
  1987. iocb->ki_pos - 1);
  1988. if (ret < 0)
  1989. written = ret;
  1990. if (!ret) {
  1991. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  1992. if (ret < 0)
  1993. written = ret;
  1994. }
  1995. if (!ret)
  1996. ret = filemap_fdatawait_range(file->f_mapping,
  1997. iocb->ki_pos - written,
  1998. iocb->ki_pos - 1);
  1999. }
  2000. out:
  2001. if (saved_ki_complete)
  2002. xchg(&iocb->ki_complete, saved_ki_complete);
  2003. if (rw_level != -1)
  2004. ocfs2_rw_unlock(inode, rw_level);
  2005. out_mutex:
  2006. inode_unlock(inode);
  2007. if (written)
  2008. ret = written;
  2009. return ret;
  2010. }
  2011. static ssize_t ocfs2_file_read_iter(struct kiocb *iocb,
  2012. struct iov_iter *to)
  2013. {
  2014. int ret = 0, rw_level = -1, lock_level = 0;
  2015. struct file *filp = iocb->ki_filp;
  2016. struct inode *inode = file_inode(filp);
  2017. trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry,
  2018. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2019. filp->f_path.dentry->d_name.len,
  2020. filp->f_path.dentry->d_name.name,
  2021. to->nr_segs); /* GRRRRR */
  2022. if (!inode) {
  2023. ret = -EINVAL;
  2024. mlog_errno(ret);
  2025. goto bail;
  2026. }
  2027. /*
  2028. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  2029. * need locks to protect pending reads from racing with truncate.
  2030. */
  2031. if (iocb->ki_flags & IOCB_DIRECT) {
  2032. ret = ocfs2_rw_lock(inode, 0);
  2033. if (ret < 0) {
  2034. mlog_errno(ret);
  2035. goto bail;
  2036. }
  2037. rw_level = 0;
  2038. /* communicate with ocfs2_dio_end_io */
  2039. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2040. }
  2041. /*
  2042. * We're fine letting folks race truncates and extending
  2043. * writes with read across the cluster, just like they can
  2044. * locally. Hence no rw_lock during read.
  2045. *
  2046. * Take and drop the meta data lock to update inode fields
  2047. * like i_size. This allows the checks down below
  2048. * generic_file_aio_read() a chance of actually working.
  2049. */
  2050. ret = ocfs2_inode_lock_atime(inode, filp->f_path.mnt, &lock_level);
  2051. if (ret < 0) {
  2052. mlog_errno(ret);
  2053. goto bail;
  2054. }
  2055. ocfs2_inode_unlock(inode, lock_level);
  2056. ret = generic_file_read_iter(iocb, to);
  2057. trace_generic_file_aio_read_ret(ret);
  2058. /* buffered aio wouldn't have proper lock coverage today */
  2059. BUG_ON(ret == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
  2060. /* see ocfs2_file_write_iter */
  2061. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  2062. rw_level = -1;
  2063. }
  2064. bail:
  2065. if (rw_level != -1)
  2066. ocfs2_rw_unlock(inode, rw_level);
  2067. return ret;
  2068. }
  2069. /* Refer generic_file_llseek_unlocked() */
  2070. static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence)
  2071. {
  2072. struct inode *inode = file->f_mapping->host;
  2073. int ret = 0;
  2074. inode_lock(inode);
  2075. switch (whence) {
  2076. case SEEK_SET:
  2077. break;
  2078. case SEEK_END:
  2079. /* SEEK_END requires the OCFS2 inode lock for the file
  2080. * because it references the file's size.
  2081. */
  2082. ret = ocfs2_inode_lock(inode, NULL, 0);
  2083. if (ret < 0) {
  2084. mlog_errno(ret);
  2085. goto out;
  2086. }
  2087. offset += i_size_read(inode);
  2088. ocfs2_inode_unlock(inode, 0);
  2089. break;
  2090. case SEEK_CUR:
  2091. if (offset == 0) {
  2092. offset = file->f_pos;
  2093. goto out;
  2094. }
  2095. offset += file->f_pos;
  2096. break;
  2097. case SEEK_DATA:
  2098. case SEEK_HOLE:
  2099. ret = ocfs2_seek_data_hole_offset(file, &offset, whence);
  2100. if (ret)
  2101. goto out;
  2102. break;
  2103. default:
  2104. ret = -EINVAL;
  2105. goto out;
  2106. }
  2107. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  2108. out:
  2109. inode_unlock(inode);
  2110. if (ret)
  2111. return ret;
  2112. return offset;
  2113. }
  2114. static int ocfs2_file_clone_range(struct file *file_in,
  2115. loff_t pos_in,
  2116. struct file *file_out,
  2117. loff_t pos_out,
  2118. u64 len)
  2119. {
  2120. return ocfs2_reflink_remap_range(file_in, pos_in, file_out, pos_out,
  2121. len, false);
  2122. }
  2123. static ssize_t ocfs2_file_dedupe_range(struct file *src_file,
  2124. u64 loff,
  2125. u64 len,
  2126. struct file *dst_file,
  2127. u64 dst_loff)
  2128. {
  2129. int error;
  2130. error = ocfs2_reflink_remap_range(src_file, loff, dst_file, dst_loff,
  2131. len, true);
  2132. if (error)
  2133. return error;
  2134. return len;
  2135. }
  2136. const struct inode_operations ocfs2_file_iops = {
  2137. .setattr = ocfs2_setattr,
  2138. .getattr = ocfs2_getattr,
  2139. .permission = ocfs2_permission,
  2140. .listxattr = ocfs2_listxattr,
  2141. .fiemap = ocfs2_fiemap,
  2142. .get_acl = ocfs2_iop_get_acl,
  2143. .set_acl = ocfs2_iop_set_acl,
  2144. };
  2145. const struct inode_operations ocfs2_special_file_iops = {
  2146. .setattr = ocfs2_setattr,
  2147. .getattr = ocfs2_getattr,
  2148. .permission = ocfs2_permission,
  2149. .get_acl = ocfs2_iop_get_acl,
  2150. .set_acl = ocfs2_iop_set_acl,
  2151. };
  2152. /*
  2153. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  2154. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  2155. */
  2156. const struct file_operations ocfs2_fops = {
  2157. .llseek = ocfs2_file_llseek,
  2158. .mmap = ocfs2_mmap,
  2159. .fsync = ocfs2_sync_file,
  2160. .release = ocfs2_file_release,
  2161. .open = ocfs2_file_open,
  2162. .read_iter = ocfs2_file_read_iter,
  2163. .write_iter = ocfs2_file_write_iter,
  2164. .unlocked_ioctl = ocfs2_ioctl,
  2165. #ifdef CONFIG_COMPAT
  2166. .compat_ioctl = ocfs2_compat_ioctl,
  2167. #endif
  2168. .lock = ocfs2_lock,
  2169. .flock = ocfs2_flock,
  2170. .splice_read = generic_file_splice_read,
  2171. .splice_write = iter_file_splice_write,
  2172. .fallocate = ocfs2_fallocate,
  2173. .clone_file_range = ocfs2_file_clone_range,
  2174. .dedupe_file_range = ocfs2_file_dedupe_range,
  2175. };
  2176. const struct file_operations ocfs2_dops = {
  2177. .llseek = generic_file_llseek,
  2178. .read = generic_read_dir,
  2179. .iterate = ocfs2_readdir,
  2180. .fsync = ocfs2_sync_file,
  2181. .release = ocfs2_dir_release,
  2182. .open = ocfs2_dir_open,
  2183. .unlocked_ioctl = ocfs2_ioctl,
  2184. #ifdef CONFIG_COMPAT
  2185. .compat_ioctl = ocfs2_compat_ioctl,
  2186. #endif
  2187. .lock = ocfs2_lock,
  2188. .flock = ocfs2_flock,
  2189. };
  2190. /*
  2191. * POSIX-lockless variants of our file_operations.
  2192. *
  2193. * These will be used if the underlying cluster stack does not support
  2194. * posix file locking, if the user passes the "localflocks" mount
  2195. * option, or if we have a local-only fs.
  2196. *
  2197. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  2198. * so we still want it in the case of no stack support for
  2199. * plocks. Internally, it will do the right thing when asked to ignore
  2200. * the cluster.
  2201. */
  2202. const struct file_operations ocfs2_fops_no_plocks = {
  2203. .llseek = ocfs2_file_llseek,
  2204. .mmap = ocfs2_mmap,
  2205. .fsync = ocfs2_sync_file,
  2206. .release = ocfs2_file_release,
  2207. .open = ocfs2_file_open,
  2208. .read_iter = ocfs2_file_read_iter,
  2209. .write_iter = ocfs2_file_write_iter,
  2210. .unlocked_ioctl = ocfs2_ioctl,
  2211. #ifdef CONFIG_COMPAT
  2212. .compat_ioctl = ocfs2_compat_ioctl,
  2213. #endif
  2214. .flock = ocfs2_flock,
  2215. .splice_read = generic_file_splice_read,
  2216. .splice_write = iter_file_splice_write,
  2217. .fallocate = ocfs2_fallocate,
  2218. .clone_file_range = ocfs2_file_clone_range,
  2219. .dedupe_file_range = ocfs2_file_dedupe_range,
  2220. };
  2221. const struct file_operations ocfs2_dops_no_plocks = {
  2222. .llseek = generic_file_llseek,
  2223. .read = generic_read_dir,
  2224. .iterate = ocfs2_readdir,
  2225. .fsync = ocfs2_sync_file,
  2226. .release = ocfs2_dir_release,
  2227. .open = ocfs2_dir_open,
  2228. .unlocked_ioctl = ocfs2_ioctl,
  2229. #ifdef CONFIG_COMPAT
  2230. .compat_ioctl = ocfs2_compat_ioctl,
  2231. #endif
  2232. .flock = ocfs2_flock,
  2233. };