ops_inode.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/swap.h>
  21. #include <linux/falloc.h>
  22. #include <asm/uaccess.h>
  23. #include "gfs2.h"
  24. #include "incore.h"
  25. #include "acl.h"
  26. #include "bmap.h"
  27. #include "dir.h"
  28. #include "xattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "quota.h"
  33. #include "rgrp.h"
  34. #include "trans.h"
  35. #include "util.h"
  36. #include "super.h"
  37. /**
  38. * gfs2_create - Create a file
  39. * @dir: The directory in which to create the file
  40. * @dentry: The dentry of the new file
  41. * @mode: The mode of the new file
  42. *
  43. * Returns: errno
  44. */
  45. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  46. int mode, struct nameidata *nd)
  47. {
  48. struct gfs2_inode *dip = GFS2_I(dir);
  49. struct gfs2_sbd *sdp = GFS2_SB(dir);
  50. struct gfs2_holder ghs[2];
  51. struct inode *inode;
  52. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  53. for (;;) {
  54. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  55. if (!IS_ERR(inode)) {
  56. gfs2_trans_end(sdp);
  57. if (dip->i_alloc->al_rgd)
  58. gfs2_inplace_release(dip);
  59. gfs2_quota_unlock(dip);
  60. gfs2_alloc_put(dip);
  61. gfs2_glock_dq_uninit_m(2, ghs);
  62. mark_inode_dirty(inode);
  63. break;
  64. } else if (PTR_ERR(inode) != -EEXIST ||
  65. (nd && nd->flags & LOOKUP_EXCL)) {
  66. gfs2_holder_uninit(ghs);
  67. return PTR_ERR(inode);
  68. }
  69. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  70. if (inode) {
  71. if (!IS_ERR(inode)) {
  72. gfs2_holder_uninit(ghs);
  73. break;
  74. } else {
  75. gfs2_holder_uninit(ghs);
  76. return PTR_ERR(inode);
  77. }
  78. }
  79. }
  80. d_instantiate(dentry, inode);
  81. return 0;
  82. }
  83. /**
  84. * gfs2_lookup - Look up a filename in a directory and return its inode
  85. * @dir: The directory inode
  86. * @dentry: The dentry of the new inode
  87. * @nd: passed from Linux VFS, ignored by us
  88. *
  89. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  90. *
  91. * Returns: errno
  92. */
  93. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  94. struct nameidata *nd)
  95. {
  96. struct inode *inode = NULL;
  97. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  98. if (inode && IS_ERR(inode))
  99. return ERR_CAST(inode);
  100. if (inode) {
  101. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  102. struct gfs2_holder gh;
  103. int error;
  104. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  105. if (error) {
  106. iput(inode);
  107. return ERR_PTR(error);
  108. }
  109. gfs2_glock_dq_uninit(&gh);
  110. return d_splice_alias(inode, dentry);
  111. }
  112. d_add(dentry, inode);
  113. return NULL;
  114. }
  115. /**
  116. * gfs2_link - Link to a file
  117. * @old_dentry: The inode to link
  118. * @dir: Add link to this directory
  119. * @dentry: The name of the link
  120. *
  121. * Link the inode in "old_dentry" into the directory "dir" with the
  122. * name in "dentry".
  123. *
  124. * Returns: errno
  125. */
  126. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  127. struct dentry *dentry)
  128. {
  129. struct gfs2_inode *dip = GFS2_I(dir);
  130. struct gfs2_sbd *sdp = GFS2_SB(dir);
  131. struct inode *inode = old_dentry->d_inode;
  132. struct gfs2_inode *ip = GFS2_I(inode);
  133. struct gfs2_holder ghs[2];
  134. int alloc_required;
  135. int error;
  136. if (S_ISDIR(inode->i_mode))
  137. return -EPERM;
  138. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  139. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  140. error = gfs2_glock_nq(ghs); /* parent */
  141. if (error)
  142. goto out_parent;
  143. error = gfs2_glock_nq(ghs + 1); /* child */
  144. if (error)
  145. goto out_child;
  146. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0);
  147. if (error)
  148. goto out_gunlock;
  149. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  150. switch (error) {
  151. case -ENOENT:
  152. break;
  153. case 0:
  154. error = -EEXIST;
  155. default:
  156. goto out_gunlock;
  157. }
  158. error = -EINVAL;
  159. if (!dip->i_inode.i_nlink)
  160. goto out_gunlock;
  161. error = -EFBIG;
  162. if (dip->i_entries == (u32)-1)
  163. goto out_gunlock;
  164. error = -EPERM;
  165. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  166. goto out_gunlock;
  167. error = -EINVAL;
  168. if (!ip->i_inode.i_nlink)
  169. goto out_gunlock;
  170. error = -EMLINK;
  171. if (ip->i_inode.i_nlink == (u32)-1)
  172. goto out_gunlock;
  173. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  174. if (error < 0)
  175. goto out_gunlock;
  176. error = 0;
  177. if (alloc_required) {
  178. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  179. if (!al) {
  180. error = -ENOMEM;
  181. goto out_gunlock;
  182. }
  183. error = gfs2_quota_lock_check(dip);
  184. if (error)
  185. goto out_alloc;
  186. al->al_requested = sdp->sd_max_dirres;
  187. error = gfs2_inplace_reserve(dip);
  188. if (error)
  189. goto out_gunlock_q;
  190. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  191. gfs2_rg_blocks(al) +
  192. 2 * RES_DINODE + RES_STATFS +
  193. RES_QUOTA, 0);
  194. if (error)
  195. goto out_ipres;
  196. } else {
  197. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  198. if (error)
  199. goto out_ipres;
  200. }
  201. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  202. if (error)
  203. goto out_end_trans;
  204. error = gfs2_change_nlink(ip, +1);
  205. out_end_trans:
  206. gfs2_trans_end(sdp);
  207. out_ipres:
  208. if (alloc_required)
  209. gfs2_inplace_release(dip);
  210. out_gunlock_q:
  211. if (alloc_required)
  212. gfs2_quota_unlock(dip);
  213. out_alloc:
  214. if (alloc_required)
  215. gfs2_alloc_put(dip);
  216. out_gunlock:
  217. gfs2_glock_dq(ghs + 1);
  218. out_child:
  219. gfs2_glock_dq(ghs);
  220. out_parent:
  221. gfs2_holder_uninit(ghs);
  222. gfs2_holder_uninit(ghs + 1);
  223. if (!error) {
  224. ihold(inode);
  225. d_instantiate(dentry, inode);
  226. mark_inode_dirty(inode);
  227. }
  228. return error;
  229. }
  230. /*
  231. * gfs2_unlink_ok - check to see that a inode is still in a directory
  232. * @dip: the directory
  233. * @name: the name of the file
  234. * @ip: the inode
  235. *
  236. * Assumes that the lock on (at least) @dip is held.
  237. *
  238. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  239. */
  240. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  241. const struct gfs2_inode *ip)
  242. {
  243. int error;
  244. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  245. return -EPERM;
  246. if ((dip->i_inode.i_mode & S_ISVTX) &&
  247. dip->i_inode.i_uid != current_fsuid() &&
  248. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  249. return -EPERM;
  250. if (IS_APPEND(&dip->i_inode))
  251. return -EPERM;
  252. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
  253. if (error)
  254. return error;
  255. error = gfs2_dir_check(&dip->i_inode, name, ip);
  256. if (error)
  257. return error;
  258. return 0;
  259. }
  260. /**
  261. * gfs2_unlink - Unlink a file
  262. * @dir: The inode of the directory containing the file to unlink
  263. * @dentry: The file itself
  264. *
  265. * Unlink a file. Call gfs2_unlinki()
  266. *
  267. * Returns: errno
  268. */
  269. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  270. {
  271. struct gfs2_inode *dip = GFS2_I(dir);
  272. struct gfs2_sbd *sdp = GFS2_SB(dir);
  273. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  274. struct gfs2_holder ghs[3];
  275. struct gfs2_rgrpd *rgd;
  276. struct gfs2_holder ri_gh;
  277. int error;
  278. error = gfs2_rindex_hold(sdp, &ri_gh);
  279. if (error)
  280. return error;
  281. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  282. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  283. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  284. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  285. error = gfs2_glock_nq(ghs); /* parent */
  286. if (error)
  287. goto out_parent;
  288. error = gfs2_glock_nq(ghs + 1); /* child */
  289. if (error)
  290. goto out_child;
  291. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  292. if (error)
  293. goto out_rgrp;
  294. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  295. if (error)
  296. goto out_gunlock;
  297. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  298. if (error)
  299. goto out_gunlock;
  300. error = gfs2_dir_del(dip, &dentry->d_name);
  301. if (error)
  302. goto out_end_trans;
  303. error = gfs2_change_nlink(ip, -1);
  304. out_end_trans:
  305. gfs2_trans_end(sdp);
  306. out_gunlock:
  307. gfs2_glock_dq(ghs + 2);
  308. out_rgrp:
  309. gfs2_holder_uninit(ghs + 2);
  310. gfs2_glock_dq(ghs + 1);
  311. out_child:
  312. gfs2_holder_uninit(ghs + 1);
  313. gfs2_glock_dq(ghs);
  314. out_parent:
  315. gfs2_holder_uninit(ghs);
  316. gfs2_glock_dq_uninit(&ri_gh);
  317. return error;
  318. }
  319. /**
  320. * gfs2_symlink - Create a symlink
  321. * @dir: The directory to create the symlink in
  322. * @dentry: The dentry to put the symlink in
  323. * @symname: The thing which the link points to
  324. *
  325. * Returns: errno
  326. */
  327. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  328. const char *symname)
  329. {
  330. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  331. struct gfs2_sbd *sdp = GFS2_SB(dir);
  332. struct gfs2_holder ghs[2];
  333. struct inode *inode;
  334. struct buffer_head *dibh;
  335. int size;
  336. int error;
  337. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  338. size = strlen(symname);
  339. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  340. return -ENAMETOOLONG;
  341. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  342. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  343. if (IS_ERR(inode)) {
  344. gfs2_holder_uninit(ghs);
  345. return PTR_ERR(inode);
  346. }
  347. ip = ghs[1].gh_gl->gl_object;
  348. i_size_write(inode, size);
  349. error = gfs2_meta_inode_buffer(ip, &dibh);
  350. if (!gfs2_assert_withdraw(sdp, !error)) {
  351. gfs2_dinode_out(ip, dibh->b_data);
  352. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  353. size);
  354. brelse(dibh);
  355. }
  356. gfs2_trans_end(sdp);
  357. if (dip->i_alloc->al_rgd)
  358. gfs2_inplace_release(dip);
  359. gfs2_quota_unlock(dip);
  360. gfs2_alloc_put(dip);
  361. gfs2_glock_dq_uninit_m(2, ghs);
  362. d_instantiate(dentry, inode);
  363. mark_inode_dirty(inode);
  364. return 0;
  365. }
  366. /**
  367. * gfs2_mkdir - Make a directory
  368. * @dir: The parent directory of the new one
  369. * @dentry: The dentry of the new directory
  370. * @mode: The mode of the new directory
  371. *
  372. * Returns: errno
  373. */
  374. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  375. {
  376. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  377. struct gfs2_sbd *sdp = GFS2_SB(dir);
  378. struct gfs2_holder ghs[2];
  379. struct inode *inode;
  380. struct buffer_head *dibh;
  381. int error;
  382. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  383. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  384. if (IS_ERR(inode)) {
  385. gfs2_holder_uninit(ghs);
  386. return PTR_ERR(inode);
  387. }
  388. ip = ghs[1].gh_gl->gl_object;
  389. ip->i_inode.i_nlink = 2;
  390. i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  391. ip->i_diskflags |= GFS2_DIF_JDATA;
  392. ip->i_entries = 2;
  393. error = gfs2_meta_inode_buffer(ip, &dibh);
  394. if (!gfs2_assert_withdraw(sdp, !error)) {
  395. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  396. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  397. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  398. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  399. dent->de_inum = di->di_num; /* already GFS2 endian */
  400. dent->de_type = cpu_to_be16(DT_DIR);
  401. di->di_entries = cpu_to_be32(1);
  402. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  403. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  404. gfs2_inum_out(dip, dent);
  405. dent->de_type = cpu_to_be16(DT_DIR);
  406. gfs2_dinode_out(ip, di);
  407. brelse(dibh);
  408. }
  409. error = gfs2_change_nlink(dip, +1);
  410. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  411. gfs2_trans_end(sdp);
  412. if (dip->i_alloc->al_rgd)
  413. gfs2_inplace_release(dip);
  414. gfs2_quota_unlock(dip);
  415. gfs2_alloc_put(dip);
  416. gfs2_glock_dq_uninit_m(2, ghs);
  417. d_instantiate(dentry, inode);
  418. mark_inode_dirty(inode);
  419. return 0;
  420. }
  421. /**
  422. * gfs2_rmdiri - Remove a directory
  423. * @dip: The parent directory of the directory to be removed
  424. * @name: The name of the directory to be removed
  425. * @ip: The GFS2 inode of the directory to be removed
  426. *
  427. * Assumes Glocks on dip and ip are held
  428. *
  429. * Returns: errno
  430. */
  431. static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  432. struct gfs2_inode *ip)
  433. {
  434. int error;
  435. if (ip->i_entries != 2) {
  436. if (gfs2_consist_inode(ip))
  437. gfs2_dinode_print(ip);
  438. return -EIO;
  439. }
  440. error = gfs2_dir_del(dip, name);
  441. if (error)
  442. return error;
  443. error = gfs2_change_nlink(dip, -1);
  444. if (error)
  445. return error;
  446. error = gfs2_dir_del(ip, &gfs2_qdot);
  447. if (error)
  448. return error;
  449. error = gfs2_dir_del(ip, &gfs2_qdotdot);
  450. if (error)
  451. return error;
  452. /* It looks odd, but it really should be done twice */
  453. error = gfs2_change_nlink(ip, -1);
  454. if (error)
  455. return error;
  456. error = gfs2_change_nlink(ip, -1);
  457. if (error)
  458. return error;
  459. return error;
  460. }
  461. /**
  462. * gfs2_rmdir - Remove a directory
  463. * @dir: The parent directory of the directory to be removed
  464. * @dentry: The dentry of the directory to remove
  465. *
  466. * Remove a directory. Call gfs2_rmdiri()
  467. *
  468. * Returns: errno
  469. */
  470. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  471. {
  472. struct gfs2_inode *dip = GFS2_I(dir);
  473. struct gfs2_sbd *sdp = GFS2_SB(dir);
  474. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  475. struct gfs2_holder ghs[3];
  476. struct gfs2_rgrpd *rgd;
  477. struct gfs2_holder ri_gh;
  478. int error;
  479. error = gfs2_rindex_hold(sdp, &ri_gh);
  480. if (error)
  481. return error;
  482. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  483. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  484. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  485. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  486. error = gfs2_glock_nq(ghs); /* parent */
  487. if (error)
  488. goto out_parent;
  489. error = gfs2_glock_nq(ghs + 1); /* child */
  490. if (error)
  491. goto out_child;
  492. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  493. if (error)
  494. goto out_rgrp;
  495. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  496. if (error)
  497. goto out_gunlock;
  498. if (ip->i_entries < 2) {
  499. if (gfs2_consist_inode(ip))
  500. gfs2_dinode_print(ip);
  501. error = -EIO;
  502. goto out_gunlock;
  503. }
  504. if (ip->i_entries > 2) {
  505. error = -ENOTEMPTY;
  506. goto out_gunlock;
  507. }
  508. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  509. if (error)
  510. goto out_gunlock;
  511. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  512. gfs2_trans_end(sdp);
  513. out_gunlock:
  514. gfs2_glock_dq(ghs + 2);
  515. out_rgrp:
  516. gfs2_holder_uninit(ghs + 2);
  517. gfs2_glock_dq(ghs + 1);
  518. out_child:
  519. gfs2_holder_uninit(ghs + 1);
  520. gfs2_glock_dq(ghs);
  521. out_parent:
  522. gfs2_holder_uninit(ghs);
  523. gfs2_glock_dq_uninit(&ri_gh);
  524. return error;
  525. }
  526. /**
  527. * gfs2_mknod - Make a special file
  528. * @dir: The directory in which the special file will reside
  529. * @dentry: The dentry of the special file
  530. * @mode: The mode of the special file
  531. * @rdev: The device specification of the special file
  532. *
  533. */
  534. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  535. dev_t dev)
  536. {
  537. struct gfs2_inode *dip = GFS2_I(dir);
  538. struct gfs2_sbd *sdp = GFS2_SB(dir);
  539. struct gfs2_holder ghs[2];
  540. struct inode *inode;
  541. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  542. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  543. if (IS_ERR(inode)) {
  544. gfs2_holder_uninit(ghs);
  545. return PTR_ERR(inode);
  546. }
  547. gfs2_trans_end(sdp);
  548. if (dip->i_alloc->al_rgd)
  549. gfs2_inplace_release(dip);
  550. gfs2_quota_unlock(dip);
  551. gfs2_alloc_put(dip);
  552. gfs2_glock_dq_uninit_m(2, ghs);
  553. d_instantiate(dentry, inode);
  554. mark_inode_dirty(inode);
  555. return 0;
  556. }
  557. /*
  558. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  559. * @this: move this
  560. * @to: to here
  561. *
  562. * Follow @to back to the root and make sure we don't encounter @this
  563. * Assumes we already hold the rename lock.
  564. *
  565. * Returns: errno
  566. */
  567. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  568. {
  569. struct inode *dir = &to->i_inode;
  570. struct super_block *sb = dir->i_sb;
  571. struct inode *tmp;
  572. int error = 0;
  573. igrab(dir);
  574. for (;;) {
  575. if (dir == &this->i_inode) {
  576. error = -EINVAL;
  577. break;
  578. }
  579. if (dir == sb->s_root->d_inode) {
  580. error = 0;
  581. break;
  582. }
  583. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  584. if (IS_ERR(tmp)) {
  585. error = PTR_ERR(tmp);
  586. break;
  587. }
  588. iput(dir);
  589. dir = tmp;
  590. }
  591. iput(dir);
  592. return error;
  593. }
  594. /**
  595. * gfs2_rename - Rename a file
  596. * @odir: Parent directory of old file name
  597. * @odentry: The old dentry of the file
  598. * @ndir: Parent directory of new file name
  599. * @ndentry: The new dentry of the file
  600. *
  601. * Returns: errno
  602. */
  603. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  604. struct inode *ndir, struct dentry *ndentry)
  605. {
  606. struct gfs2_inode *odip = GFS2_I(odir);
  607. struct gfs2_inode *ndip = GFS2_I(ndir);
  608. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  609. struct gfs2_inode *nip = NULL;
  610. struct gfs2_sbd *sdp = GFS2_SB(odir);
  611. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
  612. struct gfs2_rgrpd *nrgd;
  613. unsigned int num_gh;
  614. int dir_rename = 0;
  615. int alloc_required = 0;
  616. unsigned int x;
  617. int error;
  618. if (ndentry->d_inode) {
  619. nip = GFS2_I(ndentry->d_inode);
  620. if (ip == nip)
  621. return 0;
  622. }
  623. error = gfs2_rindex_hold(sdp, &ri_gh);
  624. if (error)
  625. return error;
  626. if (odip != ndip) {
  627. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  628. 0, &r_gh);
  629. if (error)
  630. goto out;
  631. if (S_ISDIR(ip->i_inode.i_mode)) {
  632. dir_rename = 1;
  633. /* don't move a dirctory into it's subdir */
  634. error = gfs2_ok_to_move(ip, ndip);
  635. if (error)
  636. goto out_gunlock_r;
  637. }
  638. }
  639. num_gh = 1;
  640. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  641. if (odip != ndip) {
  642. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  643. num_gh++;
  644. }
  645. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  646. num_gh++;
  647. if (nip) {
  648. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  649. num_gh++;
  650. /* grab the resource lock for unlink flag twiddling
  651. * this is the case of the target file already existing
  652. * so we unlink before doing the rename
  653. */
  654. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  655. if (nrgd)
  656. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  657. }
  658. for (x = 0; x < num_gh; x++) {
  659. error = gfs2_glock_nq(ghs + x);
  660. if (error)
  661. goto out_gunlock;
  662. }
  663. /* Check out the old directory */
  664. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  665. if (error)
  666. goto out_gunlock;
  667. /* Check out the new directory */
  668. if (nip) {
  669. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  670. if (error)
  671. goto out_gunlock;
  672. if (S_ISDIR(nip->i_inode.i_mode)) {
  673. if (nip->i_entries < 2) {
  674. if (gfs2_consist_inode(nip))
  675. gfs2_dinode_print(nip);
  676. error = -EIO;
  677. goto out_gunlock;
  678. }
  679. if (nip->i_entries > 2) {
  680. error = -ENOTEMPTY;
  681. goto out_gunlock;
  682. }
  683. }
  684. } else {
  685. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
  686. if (error)
  687. goto out_gunlock;
  688. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  689. switch (error) {
  690. case -ENOENT:
  691. error = 0;
  692. break;
  693. case 0:
  694. error = -EEXIST;
  695. default:
  696. goto out_gunlock;
  697. };
  698. if (odip != ndip) {
  699. if (!ndip->i_inode.i_nlink) {
  700. error = -EINVAL;
  701. goto out_gunlock;
  702. }
  703. if (ndip->i_entries == (u32)-1) {
  704. error = -EFBIG;
  705. goto out_gunlock;
  706. }
  707. if (S_ISDIR(ip->i_inode.i_mode) &&
  708. ndip->i_inode.i_nlink == (u32)-1) {
  709. error = -EMLINK;
  710. goto out_gunlock;
  711. }
  712. }
  713. }
  714. /* Check out the dir to be renamed */
  715. if (dir_rename) {
  716. error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
  717. if (error)
  718. goto out_gunlock;
  719. }
  720. if (nip == NULL)
  721. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  722. error = alloc_required;
  723. if (error < 0)
  724. goto out_gunlock;
  725. error = 0;
  726. if (alloc_required) {
  727. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  728. if (!al) {
  729. error = -ENOMEM;
  730. goto out_gunlock;
  731. }
  732. error = gfs2_quota_lock_check(ndip);
  733. if (error)
  734. goto out_alloc;
  735. al->al_requested = sdp->sd_max_dirres;
  736. error = gfs2_inplace_reserve_ri(ndip);
  737. if (error)
  738. goto out_gunlock_q;
  739. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  740. gfs2_rg_blocks(al) +
  741. 4 * RES_DINODE + 4 * RES_LEAF +
  742. RES_STATFS + RES_QUOTA + 4, 0);
  743. if (error)
  744. goto out_ipreserv;
  745. } else {
  746. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  747. 5 * RES_LEAF + 4, 0);
  748. if (error)
  749. goto out_gunlock;
  750. }
  751. /* Remove the target file, if it exists */
  752. if (nip) {
  753. if (S_ISDIR(nip->i_inode.i_mode))
  754. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  755. else {
  756. error = gfs2_dir_del(ndip, &ndentry->d_name);
  757. if (error)
  758. goto out_end_trans;
  759. error = gfs2_change_nlink(nip, -1);
  760. }
  761. if (error)
  762. goto out_end_trans;
  763. }
  764. if (dir_rename) {
  765. error = gfs2_change_nlink(ndip, +1);
  766. if (error)
  767. goto out_end_trans;
  768. error = gfs2_change_nlink(odip, -1);
  769. if (error)
  770. goto out_end_trans;
  771. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  772. if (error)
  773. goto out_end_trans;
  774. } else {
  775. struct buffer_head *dibh;
  776. error = gfs2_meta_inode_buffer(ip, &dibh);
  777. if (error)
  778. goto out_end_trans;
  779. ip->i_inode.i_ctime = CURRENT_TIME;
  780. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  781. gfs2_dinode_out(ip, dibh->b_data);
  782. brelse(dibh);
  783. }
  784. error = gfs2_dir_del(odip, &odentry->d_name);
  785. if (error)
  786. goto out_end_trans;
  787. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  788. if (error)
  789. goto out_end_trans;
  790. out_end_trans:
  791. gfs2_trans_end(sdp);
  792. out_ipreserv:
  793. if (alloc_required)
  794. gfs2_inplace_release(ndip);
  795. out_gunlock_q:
  796. if (alloc_required)
  797. gfs2_quota_unlock(ndip);
  798. out_alloc:
  799. if (alloc_required)
  800. gfs2_alloc_put(ndip);
  801. out_gunlock:
  802. while (x--) {
  803. gfs2_glock_dq(ghs + x);
  804. gfs2_holder_uninit(ghs + x);
  805. }
  806. out_gunlock_r:
  807. if (r_gh.gh_gl)
  808. gfs2_glock_dq_uninit(&r_gh);
  809. out:
  810. gfs2_glock_dq_uninit(&ri_gh);
  811. return error;
  812. }
  813. /**
  814. * gfs2_follow_link - Follow a symbolic link
  815. * @dentry: The dentry of the link
  816. * @nd: Data that we pass to vfs_follow_link()
  817. *
  818. * This can handle symlinks of any size.
  819. *
  820. * Returns: 0 on success or error code
  821. */
  822. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  823. {
  824. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  825. struct gfs2_holder i_gh;
  826. struct buffer_head *dibh;
  827. unsigned int x, size;
  828. char *buf;
  829. int error;
  830. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  831. error = gfs2_glock_nq(&i_gh);
  832. if (error) {
  833. gfs2_holder_uninit(&i_gh);
  834. nd_set_link(nd, ERR_PTR(error));
  835. return NULL;
  836. }
  837. size = (unsigned int)i_size_read(&ip->i_inode);
  838. if (size == 0) {
  839. gfs2_consist_inode(ip);
  840. buf = ERR_PTR(-EIO);
  841. goto out;
  842. }
  843. error = gfs2_meta_inode_buffer(ip, &dibh);
  844. if (error) {
  845. buf = ERR_PTR(error);
  846. goto out;
  847. }
  848. x = size + 1;
  849. buf = kmalloc(x, GFP_NOFS);
  850. if (!buf)
  851. buf = ERR_PTR(-ENOMEM);
  852. else
  853. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  854. brelse(dibh);
  855. out:
  856. gfs2_glock_dq_uninit(&i_gh);
  857. nd_set_link(nd, buf);
  858. return NULL;
  859. }
  860. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  861. {
  862. char *s = nd_get_link(nd);
  863. if (!IS_ERR(s))
  864. kfree(s);
  865. }
  866. /**
  867. * gfs2_permission -
  868. * @inode:
  869. * @mask:
  870. * @nd: passed from Linux VFS, ignored by us
  871. *
  872. * This may be called from the VFS directly, or from within GFS2 with the
  873. * inode locked, so we look to see if the glock is already locked and only
  874. * lock the glock if its not already been done.
  875. *
  876. * Returns: errno
  877. */
  878. int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
  879. {
  880. struct gfs2_inode *ip;
  881. struct gfs2_holder i_gh;
  882. int error;
  883. int unlock = 0;
  884. if (flags & IPERM_FLAG_RCU)
  885. return -ECHILD;
  886. ip = GFS2_I(inode);
  887. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  888. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  889. if (error)
  890. return error;
  891. unlock = 1;
  892. }
  893. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  894. error = -EACCES;
  895. else
  896. error = generic_permission(inode, mask, flags, gfs2_check_acl);
  897. if (unlock)
  898. gfs2_glock_dq_uninit(&i_gh);
  899. return error;
  900. }
  901. static int setattr_chown(struct inode *inode, struct iattr *attr)
  902. {
  903. struct gfs2_inode *ip = GFS2_I(inode);
  904. struct gfs2_sbd *sdp = GFS2_SB(inode);
  905. u32 ouid, ogid, nuid, ngid;
  906. int error;
  907. ouid = inode->i_uid;
  908. ogid = inode->i_gid;
  909. nuid = attr->ia_uid;
  910. ngid = attr->ia_gid;
  911. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  912. ouid = nuid = NO_QUOTA_CHANGE;
  913. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  914. ogid = ngid = NO_QUOTA_CHANGE;
  915. if (!gfs2_alloc_get(ip))
  916. return -ENOMEM;
  917. error = gfs2_quota_lock(ip, nuid, ngid);
  918. if (error)
  919. goto out_alloc;
  920. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  921. error = gfs2_quota_check(ip, nuid, ngid);
  922. if (error)
  923. goto out_gunlock_q;
  924. }
  925. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  926. if (error)
  927. goto out_gunlock_q;
  928. error = gfs2_setattr_simple(ip, attr);
  929. if (error)
  930. goto out_end_trans;
  931. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  932. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  933. gfs2_quota_change(ip, -blocks, ouid, ogid);
  934. gfs2_quota_change(ip, blocks, nuid, ngid);
  935. }
  936. out_end_trans:
  937. gfs2_trans_end(sdp);
  938. out_gunlock_q:
  939. gfs2_quota_unlock(ip);
  940. out_alloc:
  941. gfs2_alloc_put(ip);
  942. return error;
  943. }
  944. /**
  945. * gfs2_setattr - Change attributes on an inode
  946. * @dentry: The dentry which is changing
  947. * @attr: The structure describing the change
  948. *
  949. * The VFS layer wants to change one or more of an inodes attributes. Write
  950. * that change out to disk.
  951. *
  952. * Returns: errno
  953. */
  954. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  955. {
  956. struct inode *inode = dentry->d_inode;
  957. struct gfs2_inode *ip = GFS2_I(inode);
  958. struct gfs2_holder i_gh;
  959. int error;
  960. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  961. if (error)
  962. return error;
  963. error = -EPERM;
  964. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  965. goto out;
  966. error = inode_change_ok(inode, attr);
  967. if (error)
  968. goto out;
  969. if (attr->ia_valid & ATTR_SIZE)
  970. error = gfs2_setattr_size(inode, attr->ia_size);
  971. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  972. error = setattr_chown(inode, attr);
  973. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  974. error = gfs2_acl_chmod(ip, attr);
  975. else
  976. error = gfs2_setattr_simple(ip, attr);
  977. out:
  978. gfs2_glock_dq_uninit(&i_gh);
  979. if (!error)
  980. mark_inode_dirty(inode);
  981. return error;
  982. }
  983. /**
  984. * gfs2_getattr - Read out an inode's attributes
  985. * @mnt: The vfsmount the inode is being accessed from
  986. * @dentry: The dentry to stat
  987. * @stat: The inode's stats
  988. *
  989. * This may be called from the VFS directly, or from within GFS2 with the
  990. * inode locked, so we look to see if the glock is already locked and only
  991. * lock the glock if its not already been done. Note that its the NFS
  992. * readdirplus operation which causes this to be called (from filldir)
  993. * with the glock already held.
  994. *
  995. * Returns: errno
  996. */
  997. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  998. struct kstat *stat)
  999. {
  1000. struct inode *inode = dentry->d_inode;
  1001. struct gfs2_inode *ip = GFS2_I(inode);
  1002. struct gfs2_holder gh;
  1003. int error;
  1004. int unlock = 0;
  1005. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1006. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1007. if (error)
  1008. return error;
  1009. unlock = 1;
  1010. }
  1011. generic_fillattr(inode, stat);
  1012. if (unlock)
  1013. gfs2_glock_dq_uninit(&gh);
  1014. return 0;
  1015. }
  1016. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1017. const void *data, size_t size, int flags)
  1018. {
  1019. struct inode *inode = dentry->d_inode;
  1020. struct gfs2_inode *ip = GFS2_I(inode);
  1021. struct gfs2_holder gh;
  1022. int ret;
  1023. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1024. ret = gfs2_glock_nq(&gh);
  1025. if (ret == 0) {
  1026. ret = generic_setxattr(dentry, name, data, size, flags);
  1027. gfs2_glock_dq(&gh);
  1028. }
  1029. gfs2_holder_uninit(&gh);
  1030. return ret;
  1031. }
  1032. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1033. void *data, size_t size)
  1034. {
  1035. struct inode *inode = dentry->d_inode;
  1036. struct gfs2_inode *ip = GFS2_I(inode);
  1037. struct gfs2_holder gh;
  1038. int ret;
  1039. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1040. ret = gfs2_glock_nq(&gh);
  1041. if (ret == 0) {
  1042. ret = generic_getxattr(dentry, name, data, size);
  1043. gfs2_glock_dq(&gh);
  1044. }
  1045. gfs2_holder_uninit(&gh);
  1046. return ret;
  1047. }
  1048. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1049. {
  1050. struct inode *inode = dentry->d_inode;
  1051. struct gfs2_inode *ip = GFS2_I(inode);
  1052. struct gfs2_holder gh;
  1053. int ret;
  1054. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1055. ret = gfs2_glock_nq(&gh);
  1056. if (ret == 0) {
  1057. ret = generic_removexattr(dentry, name);
  1058. gfs2_glock_dq(&gh);
  1059. }
  1060. gfs2_holder_uninit(&gh);
  1061. return ret;
  1062. }
  1063. static void empty_write_end(struct page *page, unsigned from,
  1064. unsigned to)
  1065. {
  1066. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  1067. page_zero_new_buffers(page, from, to);
  1068. flush_dcache_page(page);
  1069. mark_page_accessed(page);
  1070. if (!gfs2_is_writeback(ip))
  1071. gfs2_page_add_databufs(ip, page, from, to);
  1072. block_commit_write(page, from, to);
  1073. }
  1074. static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
  1075. {
  1076. unsigned start, end, next;
  1077. struct buffer_head *bh, *head;
  1078. int error;
  1079. if (!page_has_buffers(page)) {
  1080. error = __block_write_begin(page, from, to - from, gfs2_block_map);
  1081. if (unlikely(error))
  1082. return error;
  1083. empty_write_end(page, from, to);
  1084. return 0;
  1085. }
  1086. bh = head = page_buffers(page);
  1087. next = end = 0;
  1088. while (next < from) {
  1089. next += bh->b_size;
  1090. bh = bh->b_this_page;
  1091. }
  1092. start = next;
  1093. do {
  1094. next += bh->b_size;
  1095. if (buffer_mapped(bh)) {
  1096. if (end) {
  1097. error = __block_write_begin(page, start, end - start,
  1098. gfs2_block_map);
  1099. if (unlikely(error))
  1100. return error;
  1101. empty_write_end(page, start, end);
  1102. end = 0;
  1103. }
  1104. start = next;
  1105. }
  1106. else
  1107. end = next;
  1108. bh = bh->b_this_page;
  1109. } while (next < to);
  1110. if (end) {
  1111. error = __block_write_begin(page, start, end - start, gfs2_block_map);
  1112. if (unlikely(error))
  1113. return error;
  1114. empty_write_end(page, start, end);
  1115. }
  1116. return 0;
  1117. }
  1118. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  1119. int mode)
  1120. {
  1121. struct gfs2_inode *ip = GFS2_I(inode);
  1122. struct buffer_head *dibh;
  1123. int error;
  1124. u64 start = offset >> PAGE_CACHE_SHIFT;
  1125. unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
  1126. u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  1127. pgoff_t curr;
  1128. struct page *page;
  1129. unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
  1130. unsigned int from, to;
  1131. if (!end_offset)
  1132. end_offset = PAGE_CACHE_SIZE;
  1133. error = gfs2_meta_inode_buffer(ip, &dibh);
  1134. if (unlikely(error))
  1135. goto out;
  1136. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1137. if (gfs2_is_stuffed(ip)) {
  1138. error = gfs2_unstuff_dinode(ip, NULL);
  1139. if (unlikely(error))
  1140. goto out;
  1141. }
  1142. curr = start;
  1143. offset = start << PAGE_CACHE_SHIFT;
  1144. from = start_offset;
  1145. to = PAGE_CACHE_SIZE;
  1146. while (curr <= end) {
  1147. page = grab_cache_page_write_begin(inode->i_mapping, curr,
  1148. AOP_FLAG_NOFS);
  1149. if (unlikely(!page)) {
  1150. error = -ENOMEM;
  1151. goto out;
  1152. }
  1153. if (curr == end)
  1154. to = end_offset;
  1155. error = write_empty_blocks(page, from, to);
  1156. if (!error && offset + to > inode->i_size &&
  1157. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1158. i_size_write(inode, offset + to);
  1159. }
  1160. unlock_page(page);
  1161. page_cache_release(page);
  1162. if (error)
  1163. goto out;
  1164. curr++;
  1165. offset += PAGE_CACHE_SIZE;
  1166. from = 0;
  1167. }
  1168. gfs2_dinode_out(ip, dibh->b_data);
  1169. mark_inode_dirty(inode);
  1170. brelse(dibh);
  1171. out:
  1172. return error;
  1173. }
  1174. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  1175. unsigned int *data_blocks, unsigned int *ind_blocks)
  1176. {
  1177. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1178. unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
  1179. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  1180. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  1181. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1182. max_data -= tmp;
  1183. }
  1184. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  1185. so it might end up with fewer data blocks */
  1186. if (max_data <= *data_blocks)
  1187. return;
  1188. *data_blocks = max_data;
  1189. *ind_blocks = max_blocks - max_data;
  1190. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  1191. if (*len > max) {
  1192. *len = max;
  1193. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  1194. }
  1195. }
  1196. static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1197. loff_t len)
  1198. {
  1199. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1200. struct gfs2_inode *ip = GFS2_I(inode);
  1201. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  1202. loff_t bytes, max_bytes;
  1203. struct gfs2_alloc *al;
  1204. int error;
  1205. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  1206. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  1207. offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
  1208. sdp->sd_sb.sb_bsize_shift;
  1209. len = next - offset;
  1210. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  1211. if (!bytes)
  1212. bytes = UINT_MAX;
  1213. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  1214. error = gfs2_glock_nq(&ip->i_gh);
  1215. if (unlikely(error))
  1216. goto out_uninit;
  1217. if (!gfs2_write_alloc_required(ip, offset, len))
  1218. goto out_unlock;
  1219. while (len > 0) {
  1220. if (len < bytes)
  1221. bytes = len;
  1222. al = gfs2_alloc_get(ip);
  1223. if (!al) {
  1224. error = -ENOMEM;
  1225. goto out_unlock;
  1226. }
  1227. error = gfs2_quota_lock_check(ip);
  1228. if (error)
  1229. goto out_alloc_put;
  1230. retry:
  1231. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  1232. al->al_requested = data_blocks + ind_blocks;
  1233. error = gfs2_inplace_reserve(ip);
  1234. if (error) {
  1235. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  1236. bytes >>= 1;
  1237. goto retry;
  1238. }
  1239. goto out_qunlock;
  1240. }
  1241. max_bytes = bytes;
  1242. calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
  1243. al->al_requested = data_blocks + ind_blocks;
  1244. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  1245. RES_RG_HDR + gfs2_rg_blocks(al);
  1246. if (gfs2_is_jdata(ip))
  1247. rblocks += data_blocks ? data_blocks : 1;
  1248. error = gfs2_trans_begin(sdp, rblocks,
  1249. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  1250. if (error)
  1251. goto out_trans_fail;
  1252. error = fallocate_chunk(inode, offset, max_bytes, mode);
  1253. gfs2_trans_end(sdp);
  1254. if (error)
  1255. goto out_trans_fail;
  1256. len -= max_bytes;
  1257. offset += max_bytes;
  1258. gfs2_inplace_release(ip);
  1259. gfs2_quota_unlock(ip);
  1260. gfs2_alloc_put(ip);
  1261. }
  1262. goto out_unlock;
  1263. out_trans_fail:
  1264. gfs2_inplace_release(ip);
  1265. out_qunlock:
  1266. gfs2_quota_unlock(ip);
  1267. out_alloc_put:
  1268. gfs2_alloc_put(ip);
  1269. out_unlock:
  1270. gfs2_glock_dq(&ip->i_gh);
  1271. out_uninit:
  1272. gfs2_holder_uninit(&ip->i_gh);
  1273. return error;
  1274. }
  1275. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1276. u64 start, u64 len)
  1277. {
  1278. struct gfs2_inode *ip = GFS2_I(inode);
  1279. struct gfs2_holder gh;
  1280. int ret;
  1281. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1282. if (ret)
  1283. return ret;
  1284. mutex_lock(&inode->i_mutex);
  1285. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1286. if (ret)
  1287. goto out;
  1288. if (gfs2_is_stuffed(ip)) {
  1289. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1290. u64 size = i_size_read(inode);
  1291. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1292. FIEMAP_EXTENT_DATA_INLINE;
  1293. phys += sizeof(struct gfs2_dinode);
  1294. phys += start;
  1295. if (start + len > size)
  1296. len = size - start;
  1297. if (start < size)
  1298. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1299. len, flags);
  1300. if (ret == 1)
  1301. ret = 0;
  1302. } else {
  1303. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1304. gfs2_block_map);
  1305. }
  1306. gfs2_glock_dq_uninit(&gh);
  1307. out:
  1308. mutex_unlock(&inode->i_mutex);
  1309. return ret;
  1310. }
  1311. const struct inode_operations gfs2_file_iops = {
  1312. .permission = gfs2_permission,
  1313. .setattr = gfs2_setattr,
  1314. .getattr = gfs2_getattr,
  1315. .setxattr = gfs2_setxattr,
  1316. .getxattr = gfs2_getxattr,
  1317. .listxattr = gfs2_listxattr,
  1318. .removexattr = gfs2_removexattr,
  1319. .fallocate = gfs2_fallocate,
  1320. .fiemap = gfs2_fiemap,
  1321. };
  1322. const struct inode_operations gfs2_dir_iops = {
  1323. .create = gfs2_create,
  1324. .lookup = gfs2_lookup,
  1325. .link = gfs2_link,
  1326. .unlink = gfs2_unlink,
  1327. .symlink = gfs2_symlink,
  1328. .mkdir = gfs2_mkdir,
  1329. .rmdir = gfs2_rmdir,
  1330. .mknod = gfs2_mknod,
  1331. .rename = gfs2_rename,
  1332. .permission = gfs2_permission,
  1333. .setattr = gfs2_setattr,
  1334. .getattr = gfs2_getattr,
  1335. .setxattr = gfs2_setxattr,
  1336. .getxattr = gfs2_getxattr,
  1337. .listxattr = gfs2_listxattr,
  1338. .removexattr = gfs2_removexattr,
  1339. .fiemap = gfs2_fiemap,
  1340. };
  1341. const struct inode_operations gfs2_symlink_iops = {
  1342. .readlink = generic_readlink,
  1343. .follow_link = gfs2_follow_link,
  1344. .put_link = gfs2_put_link,
  1345. .permission = gfs2_permission,
  1346. .setattr = gfs2_setattr,
  1347. .getattr = gfs2_getattr,
  1348. .setxattr = gfs2_setxattr,
  1349. .getxattr = gfs2_getxattr,
  1350. .listxattr = gfs2_listxattr,
  1351. .removexattr = gfs2_removexattr,
  1352. .fiemap = gfs2_fiemap,
  1353. };