ops_inode.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/namei.h>
  15. #include <linux/utsname.h>
  16. #include <linux/mm.h>
  17. #include <linux/xattr.h>
  18. #include <linux/posix_acl.h>
  19. #include <linux/gfs2_ondisk.h>
  20. #include <linux/crc32.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "lm_interface.h"
  24. #include "incore.h"
  25. #include "acl.h"
  26. #include "bmap.h"
  27. #include "dir.h"
  28. #include "eaops.h"
  29. #include "eattr.h"
  30. #include "glock.h"
  31. #include "inode.h"
  32. #include "meta_io.h"
  33. #include "ops_dentry.h"
  34. #include "ops_inode.h"
  35. #include "page.h"
  36. #include "quota.h"
  37. #include "rgrp.h"
  38. #include "trans.h"
  39. #include "util.h"
  40. /**
  41. * gfs2_create - Create a file
  42. * @dir: The directory in which to create the file
  43. * @dentry: The dentry of the new file
  44. * @mode: The mode of the new file
  45. *
  46. * Returns: errno
  47. */
  48. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  49. int mode, struct nameidata *nd)
  50. {
  51. struct gfs2_inode *dip = GFS2_I(dir);
  52. struct gfs2_sbd *sdp = GFS2_SB(dir);
  53. struct gfs2_holder ghs[2];
  54. struct inode *inode;
  55. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  56. for (;;) {
  57. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode);
  58. if (!IS_ERR(inode)) {
  59. gfs2_trans_end(sdp);
  60. if (dip->i_alloc.al_rgd)
  61. gfs2_inplace_release(dip);
  62. gfs2_quota_unlock(dip);
  63. gfs2_alloc_put(dip);
  64. gfs2_glock_dq_uninit_m(2, ghs);
  65. mark_inode_dirty(inode);
  66. break;
  67. } else if (PTR_ERR(inode) != -EEXIST ||
  68. (nd->intent.open.flags & O_EXCL)) {
  69. gfs2_holder_uninit(ghs);
  70. return PTR_ERR(inode);
  71. }
  72. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  73. if (inode) {
  74. if (!IS_ERR(inode)) {
  75. gfs2_holder_uninit(ghs);
  76. break;
  77. } else {
  78. gfs2_holder_uninit(ghs);
  79. return PTR_ERR(inode);
  80. }
  81. }
  82. }
  83. d_instantiate(dentry, inode);
  84. return 0;
  85. }
  86. /**
  87. * gfs2_lookup - Look up a filename in a directory and return its inode
  88. * @dir: The directory inode
  89. * @dentry: The dentry of the new inode
  90. * @nd: passed from Linux VFS, ignored by us
  91. *
  92. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  93. *
  94. * Returns: errno
  95. */
  96. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  97. struct nameidata *nd)
  98. {
  99. struct inode *inode = NULL;
  100. dentry->d_op = &gfs2_dops;
  101. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  102. if (inode && IS_ERR(inode))
  103. return ERR_PTR(PTR_ERR(inode));
  104. if (inode)
  105. return d_splice_alias(inode, dentry);
  106. d_add(dentry, inode);
  107. return NULL;
  108. }
  109. /**
  110. * gfs2_link - Link to a file
  111. * @old_dentry: The inode to link
  112. * @dir: Add link to this directory
  113. * @dentry: The name of the link
  114. *
  115. * Link the inode in "old_dentry" into the directory "dir" with the
  116. * name in "dentry".
  117. *
  118. * Returns: errno
  119. */
  120. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  121. struct dentry *dentry)
  122. {
  123. struct gfs2_inode *dip = GFS2_I(dir);
  124. struct gfs2_sbd *sdp = GFS2_SB(dir);
  125. struct inode *inode = old_dentry->d_inode;
  126. struct gfs2_inode *ip = GFS2_I(inode);
  127. struct gfs2_holder ghs[2];
  128. int alloc_required;
  129. int error;
  130. if (S_ISDIR(ip->i_di.di_mode))
  131. return -EPERM;
  132. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  133. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  134. error = gfs2_glock_nq_m(2, ghs);
  135. if (error)
  136. goto out;
  137. error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
  138. if (error)
  139. goto out_gunlock;
  140. error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
  141. switch (error) {
  142. case -ENOENT:
  143. break;
  144. case 0:
  145. error = -EEXIST;
  146. default:
  147. goto out_gunlock;
  148. }
  149. error = -EINVAL;
  150. if (!dip->i_di.di_nlink)
  151. goto out_gunlock;
  152. error = -EFBIG;
  153. if (dip->i_di.di_entries == (uint32_t)-1)
  154. goto out_gunlock;
  155. error = -EPERM;
  156. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  157. goto out_gunlock;
  158. error = -EINVAL;
  159. if (!ip->i_di.di_nlink)
  160. goto out_gunlock;
  161. error = -EMLINK;
  162. if (ip->i_di.di_nlink == (uint32_t)-1)
  163. goto out_gunlock;
  164. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  165. if (error < 0)
  166. goto out_gunlock;
  167. error = 0;
  168. if (alloc_required) {
  169. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  170. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  171. if (error)
  172. goto out_alloc;
  173. error = gfs2_quota_check(dip, dip->i_di.di_uid,
  174. dip->i_di.di_gid);
  175. if (error)
  176. goto out_gunlock_q;
  177. al->al_requested = sdp->sd_max_dirres;
  178. error = gfs2_inplace_reserve(dip);
  179. if (error)
  180. goto out_gunlock_q;
  181. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  182. al->al_rgd->rd_ri.ri_length +
  183. 2 * RES_DINODE + RES_STATFS +
  184. RES_QUOTA, 0);
  185. if (error)
  186. goto out_ipres;
  187. } else {
  188. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  189. if (error)
  190. goto out_ipres;
  191. }
  192. error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
  193. IF2DT(ip->i_di.di_mode));
  194. if (error)
  195. goto out_end_trans;
  196. error = gfs2_change_nlink(ip, +1);
  197. out_end_trans:
  198. gfs2_trans_end(sdp);
  199. out_ipres:
  200. if (alloc_required)
  201. gfs2_inplace_release(dip);
  202. out_gunlock_q:
  203. if (alloc_required)
  204. gfs2_quota_unlock(dip);
  205. out_alloc:
  206. if (alloc_required)
  207. gfs2_alloc_put(dip);
  208. out_gunlock:
  209. gfs2_glock_dq_m(2, ghs);
  210. out:
  211. gfs2_holder_uninit(ghs);
  212. gfs2_holder_uninit(ghs + 1);
  213. if (!error) {
  214. atomic_inc(&inode->i_count);
  215. d_instantiate(dentry, inode);
  216. mark_inode_dirty(inode);
  217. }
  218. return error;
  219. }
  220. /**
  221. * gfs2_unlink - Unlink a file
  222. * @dir: The inode of the directory containing the file to unlink
  223. * @dentry: The file itself
  224. *
  225. * Unlink a file. Call gfs2_unlinki()
  226. *
  227. * Returns: errno
  228. */
  229. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  230. {
  231. struct gfs2_inode *dip = GFS2_I(dir);
  232. struct gfs2_sbd *sdp = GFS2_SB(dir);
  233. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  234. struct gfs2_holder ghs[2];
  235. int error;
  236. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  237. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  238. error = gfs2_glock_nq_m(2, ghs);
  239. if (error)
  240. goto out;
  241. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  242. if (error)
  243. goto out_gunlock;
  244. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  245. if (error)
  246. goto out_gunlock;
  247. error = gfs2_dir_del(dip, &dentry->d_name);
  248. if (error)
  249. goto out_end_trans;
  250. error = gfs2_change_nlink(ip, -1);
  251. out_end_trans:
  252. gfs2_trans_end(sdp);
  253. out_gunlock:
  254. gfs2_glock_dq_m(2, ghs);
  255. out:
  256. gfs2_holder_uninit(ghs);
  257. gfs2_holder_uninit(ghs + 1);
  258. return error;
  259. }
  260. /**
  261. * gfs2_symlink - Create a symlink
  262. * @dir: The directory to create the symlink in
  263. * @dentry: The dentry to put the symlink in
  264. * @symname: The thing which the link points to
  265. *
  266. * Returns: errno
  267. */
  268. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  269. const char *symname)
  270. {
  271. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  272. struct gfs2_sbd *sdp = GFS2_SB(dir);
  273. struct gfs2_holder ghs[2];
  274. struct inode *inode;
  275. struct buffer_head *dibh;
  276. int size;
  277. int error;
  278. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  279. size = strlen(symname);
  280. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  281. return -ENAMETOOLONG;
  282. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  283. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO);
  284. if (IS_ERR(inode)) {
  285. gfs2_holder_uninit(ghs);
  286. return PTR_ERR(inode);
  287. }
  288. ip = ghs[1].gh_gl->gl_object;
  289. ip->i_di.di_size = size;
  290. error = gfs2_meta_inode_buffer(ip, &dibh);
  291. if (!gfs2_assert_withdraw(sdp, !error)) {
  292. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  293. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  294. size);
  295. brelse(dibh);
  296. }
  297. gfs2_trans_end(sdp);
  298. if (dip->i_alloc.al_rgd)
  299. gfs2_inplace_release(dip);
  300. gfs2_quota_unlock(dip);
  301. gfs2_alloc_put(dip);
  302. gfs2_glock_dq_uninit_m(2, ghs);
  303. d_instantiate(dentry, inode);
  304. mark_inode_dirty(inode);
  305. return 0;
  306. }
  307. /**
  308. * gfs2_mkdir - Make a directory
  309. * @dir: The parent directory of the new one
  310. * @dentry: The dentry of the new directory
  311. * @mode: The mode of the new directory
  312. *
  313. * Returns: errno
  314. */
  315. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  316. {
  317. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  318. struct gfs2_sbd *sdp = GFS2_SB(dir);
  319. struct gfs2_holder ghs[2];
  320. struct inode *inode;
  321. struct buffer_head *dibh;
  322. int error;
  323. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  324. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode);
  325. if (IS_ERR(inode)) {
  326. gfs2_holder_uninit(ghs);
  327. return PTR_ERR(inode);
  328. }
  329. ip = ghs[1].gh_gl->gl_object;
  330. ip->i_di.di_nlink = 2;
  331. ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  332. ip->i_di.di_flags |= GFS2_DIF_JDATA;
  333. ip->i_di.di_payload_format = GFS2_FORMAT_DE;
  334. ip->i_di.di_entries = 2;
  335. error = gfs2_meta_inode_buffer(ip, &dibh);
  336. if (!gfs2_assert_withdraw(sdp, !error)) {
  337. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  338. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  339. struct qstr str;
  340. gfs2_str2qstr(&str, ".");
  341. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  342. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  343. dent->de_inum = di->di_num; /* already GFS2 endian */
  344. dent->de_type = DT_DIR;
  345. di->di_entries = cpu_to_be32(1);
  346. gfs2_str2qstr(&str, "..");
  347. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  348. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  349. gfs2_inum_out(&dip->i_num, (char *) &dent->de_inum);
  350. dent->de_type = DT_DIR;
  351. gfs2_dinode_out(&ip->i_di, (char *)di);
  352. brelse(dibh);
  353. }
  354. error = gfs2_change_nlink(dip, +1);
  355. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  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_rmdir - Remove a directory
  368. * @dir: The parent directory of the directory to be removed
  369. * @dentry: The dentry of the directory to remove
  370. *
  371. * Remove a directory. Call gfs2_rmdiri()
  372. *
  373. * Returns: errno
  374. */
  375. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  376. {
  377. struct gfs2_inode *dip = GFS2_I(dir);
  378. struct gfs2_sbd *sdp = GFS2_SB(dir);
  379. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  380. struct gfs2_holder ghs[2];
  381. int error;
  382. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  383. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  384. error = gfs2_glock_nq_m(2, ghs);
  385. if (error)
  386. goto out;
  387. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  388. if (error)
  389. goto out_gunlock;
  390. if (ip->i_di.di_entries < 2) {
  391. if (gfs2_consist_inode(ip))
  392. gfs2_dinode_print(&ip->i_di);
  393. error = -EIO;
  394. goto out_gunlock;
  395. }
  396. if (ip->i_di.di_entries > 2) {
  397. error = -ENOTEMPTY;
  398. goto out_gunlock;
  399. }
  400. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  401. if (error)
  402. goto out_gunlock;
  403. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  404. gfs2_trans_end(sdp);
  405. out_gunlock:
  406. gfs2_glock_dq_m(2, ghs);
  407. out:
  408. gfs2_holder_uninit(ghs);
  409. gfs2_holder_uninit(ghs + 1);
  410. return error;
  411. }
  412. /**
  413. * gfs2_mknod - Make a special file
  414. * @dir: The directory in which the special file will reside
  415. * @dentry: The dentry of the special file
  416. * @mode: The mode of the special file
  417. * @rdev: The device specification of the special file
  418. *
  419. */
  420. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  421. dev_t dev)
  422. {
  423. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  424. struct gfs2_sbd *sdp = GFS2_SB(dir);
  425. struct gfs2_holder ghs[2];
  426. struct inode *inode;
  427. struct buffer_head *dibh;
  428. uint32_t major = 0, minor = 0;
  429. int error;
  430. switch (mode & S_IFMT) {
  431. case S_IFBLK:
  432. case S_IFCHR:
  433. major = MAJOR(dev);
  434. minor = MINOR(dev);
  435. break;
  436. case S_IFIFO:
  437. case S_IFSOCK:
  438. break;
  439. default:
  440. return -EOPNOTSUPP;
  441. };
  442. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  443. inode = gfs2_createi(ghs, &dentry->d_name, mode);
  444. if (IS_ERR(inode)) {
  445. gfs2_holder_uninit(ghs);
  446. return PTR_ERR(inode);
  447. }
  448. ip = ghs[1].gh_gl->gl_object;
  449. ip->i_di.di_major = major;
  450. ip->i_di.di_minor = minor;
  451. error = gfs2_meta_inode_buffer(ip, &dibh);
  452. if (!gfs2_assert_withdraw(sdp, !error)) {
  453. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  454. brelse(dibh);
  455. }
  456. gfs2_trans_end(sdp);
  457. if (dip->i_alloc.al_rgd)
  458. gfs2_inplace_release(dip);
  459. gfs2_quota_unlock(dip);
  460. gfs2_alloc_put(dip);
  461. gfs2_glock_dq_uninit_m(2, ghs);
  462. d_instantiate(dentry, inode);
  463. mark_inode_dirty(inode);
  464. return 0;
  465. }
  466. /**
  467. * gfs2_rename - Rename a file
  468. * @odir: Parent directory of old file name
  469. * @odentry: The old dentry of the file
  470. * @ndir: Parent directory of new file name
  471. * @ndentry: The new dentry of the file
  472. *
  473. * Returns: errno
  474. */
  475. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  476. struct inode *ndir, struct dentry *ndentry)
  477. {
  478. struct gfs2_inode *odip = GFS2_I(odir);
  479. struct gfs2_inode *ndip = GFS2_I(ndir);
  480. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  481. struct gfs2_inode *nip = NULL;
  482. struct gfs2_sbd *sdp = GFS2_SB(odir);
  483. struct gfs2_holder ghs[4], r_gh;
  484. unsigned int num_gh;
  485. int dir_rename = 0;
  486. int alloc_required;
  487. unsigned int x;
  488. int error;
  489. if (ndentry->d_inode) {
  490. nip = GFS2_I(ndentry->d_inode);
  491. if (ip == nip)
  492. return 0;
  493. }
  494. /* Make sure we aren't trying to move a dirctory into it's subdir */
  495. if (S_ISDIR(ip->i_di.di_mode) && odip != ndip) {
  496. dir_rename = 1;
  497. error = gfs2_glock_nq_init(sdp->sd_rename_gl,
  498. LM_ST_EXCLUSIVE, 0,
  499. &r_gh);
  500. if (error)
  501. goto out;
  502. error = gfs2_ok_to_move(ip, ndip);
  503. if (error)
  504. goto out_gunlock_r;
  505. }
  506. num_gh = 1;
  507. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  508. if (odip != ndip) {
  509. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  510. num_gh++;
  511. }
  512. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  513. num_gh++;
  514. if (nip) {
  515. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  516. num_gh++;
  517. }
  518. error = gfs2_glock_nq_m(num_gh, ghs);
  519. if (error)
  520. goto out_uninit;
  521. /* Check out the old directory */
  522. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  523. if (error)
  524. goto out_gunlock;
  525. /* Check out the new directory */
  526. if (nip) {
  527. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  528. if (error)
  529. goto out_gunlock;
  530. if (S_ISDIR(nip->i_di.di_mode)) {
  531. if (nip->i_di.di_entries < 2) {
  532. if (gfs2_consist_inode(nip))
  533. gfs2_dinode_print(&nip->i_di);
  534. error = -EIO;
  535. goto out_gunlock;
  536. }
  537. if (nip->i_di.di_entries > 2) {
  538. error = -ENOTEMPTY;
  539. goto out_gunlock;
  540. }
  541. }
  542. } else {
  543. error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
  544. if (error)
  545. goto out_gunlock;
  546. error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
  547. switch (error) {
  548. case -ENOENT:
  549. error = 0;
  550. break;
  551. case 0:
  552. error = -EEXIST;
  553. default:
  554. goto out_gunlock;
  555. };
  556. if (odip != ndip) {
  557. if (!ndip->i_di.di_nlink) {
  558. error = -EINVAL;
  559. goto out_gunlock;
  560. }
  561. if (ndip->i_di.di_entries == (uint32_t)-1) {
  562. error = -EFBIG;
  563. goto out_gunlock;
  564. }
  565. if (S_ISDIR(ip->i_di.di_mode) &&
  566. ndip->i_di.di_nlink == (uint32_t)-1) {
  567. error = -EMLINK;
  568. goto out_gunlock;
  569. }
  570. }
  571. }
  572. /* Check out the dir to be renamed */
  573. if (dir_rename) {
  574. error = permission(odentry->d_inode, MAY_WRITE, NULL);
  575. if (error)
  576. goto out_gunlock;
  577. }
  578. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  579. if (error < 0)
  580. goto out_gunlock;
  581. error = 0;
  582. if (alloc_required) {
  583. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  584. error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  585. if (error)
  586. goto out_alloc;
  587. error = gfs2_quota_check(ndip, ndip->i_di.di_uid,
  588. ndip->i_di.di_gid);
  589. if (error)
  590. goto out_gunlock_q;
  591. al->al_requested = sdp->sd_max_dirres;
  592. error = gfs2_inplace_reserve(ndip);
  593. if (error)
  594. goto out_gunlock_q;
  595. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  596. al->al_rgd->rd_ri.ri_length +
  597. 4 * RES_DINODE + 4 * RES_LEAF +
  598. RES_STATFS + RES_QUOTA, 0);
  599. if (error)
  600. goto out_ipreserv;
  601. } else {
  602. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  603. 5 * RES_LEAF, 0);
  604. if (error)
  605. goto out_gunlock;
  606. }
  607. /* Remove the target file, if it exists */
  608. if (nip) {
  609. if (S_ISDIR(nip->i_di.di_mode))
  610. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  611. else {
  612. error = gfs2_dir_del(ndip, &ndentry->d_name);
  613. if (error)
  614. goto out_end_trans;
  615. error = gfs2_change_nlink(nip, -1);
  616. }
  617. if (error)
  618. goto out_end_trans;
  619. }
  620. if (dir_rename) {
  621. struct qstr name;
  622. gfs2_str2qstr(&name, "..");
  623. error = gfs2_change_nlink(ndip, +1);
  624. if (error)
  625. goto out_end_trans;
  626. error = gfs2_change_nlink(odip, -1);
  627. if (error)
  628. goto out_end_trans;
  629. error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
  630. if (error)
  631. goto out_end_trans;
  632. } else {
  633. struct buffer_head *dibh;
  634. error = gfs2_meta_inode_buffer(ip, &dibh);
  635. if (error)
  636. goto out_end_trans;
  637. ip->i_di.di_ctime = get_seconds();
  638. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  639. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  640. brelse(dibh);
  641. }
  642. error = gfs2_dir_del(odip, &odentry->d_name);
  643. if (error)
  644. goto out_end_trans;
  645. error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
  646. IF2DT(ip->i_di.di_mode));
  647. if (error)
  648. goto out_end_trans;
  649. out_end_trans:
  650. gfs2_trans_end(sdp);
  651. out_ipreserv:
  652. if (alloc_required)
  653. gfs2_inplace_release(ndip);
  654. out_gunlock_q:
  655. if (alloc_required)
  656. gfs2_quota_unlock(ndip);
  657. out_alloc:
  658. if (alloc_required)
  659. gfs2_alloc_put(ndip);
  660. out_gunlock:
  661. gfs2_glock_dq_m(num_gh, ghs);
  662. out_uninit:
  663. for (x = 0; x < num_gh; x++)
  664. gfs2_holder_uninit(ghs + x);
  665. out_gunlock_r:
  666. if (dir_rename)
  667. gfs2_glock_dq_uninit(&r_gh);
  668. out:
  669. return error;
  670. }
  671. /**
  672. * gfs2_readlink - Read the value of a symlink
  673. * @dentry: the symlink
  674. * @buf: the buffer to read the symlink data into
  675. * @size: the size of the buffer
  676. *
  677. * Returns: errno
  678. */
  679. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  680. int user_size)
  681. {
  682. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  683. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  684. unsigned int len = GFS2_FAST_NAME_SIZE;
  685. int error;
  686. error = gfs2_readlinki(ip, &buf, &len);
  687. if (error)
  688. return error;
  689. if (user_size > len - 1)
  690. user_size = len - 1;
  691. if (copy_to_user(user_buf, buf, user_size))
  692. error = -EFAULT;
  693. else
  694. error = user_size;
  695. if (buf != array)
  696. kfree(buf);
  697. return error;
  698. }
  699. /**
  700. * gfs2_follow_link - Follow a symbolic link
  701. * @dentry: The dentry of the link
  702. * @nd: Data that we pass to vfs_follow_link()
  703. *
  704. * This can handle symlinks of any size. It is optimised for symlinks
  705. * under GFS2_FAST_NAME_SIZE.
  706. *
  707. * Returns: 0 on success or error code
  708. */
  709. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  710. {
  711. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  712. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  713. unsigned int len = GFS2_FAST_NAME_SIZE;
  714. int error;
  715. error = gfs2_readlinki(ip, &buf, &len);
  716. if (!error) {
  717. error = vfs_follow_link(nd, buf);
  718. if (buf != array)
  719. kfree(buf);
  720. }
  721. return ERR_PTR(error);
  722. }
  723. /**
  724. * gfs2_permission -
  725. * @inode:
  726. * @mask:
  727. * @nd: passed from Linux VFS, ignored by us
  728. *
  729. * Returns: errno
  730. */
  731. static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  732. {
  733. struct gfs2_inode *ip = GFS2_I(inode);
  734. struct gfs2_holder i_gh;
  735. int error;
  736. if (ip->i_vn == ip->i_gl->gl_vn)
  737. return generic_permission(inode, mask, gfs2_check_acl);
  738. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  739. if (!error) {
  740. error = generic_permission(inode, mask, gfs2_check_acl_locked);
  741. gfs2_glock_dq_uninit(&i_gh);
  742. }
  743. return error;
  744. }
  745. static int setattr_size(struct inode *inode, struct iattr *attr)
  746. {
  747. struct gfs2_inode *ip = GFS2_I(inode);
  748. int error;
  749. if (attr->ia_size != ip->i_di.di_size) {
  750. error = vmtruncate(inode, attr->ia_size);
  751. if (error)
  752. return error;
  753. }
  754. error = gfs2_truncatei(ip, attr->ia_size);
  755. if (error)
  756. return error;
  757. return error;
  758. }
  759. static int setattr_chown(struct inode *inode, struct iattr *attr)
  760. {
  761. struct gfs2_inode *ip = GFS2_I(inode);
  762. struct gfs2_sbd *sdp = GFS2_SB(inode);
  763. struct buffer_head *dibh;
  764. uint32_t ouid, ogid, nuid, ngid;
  765. int error;
  766. ouid = ip->i_di.di_uid;
  767. ogid = ip->i_di.di_gid;
  768. nuid = attr->ia_uid;
  769. ngid = attr->ia_gid;
  770. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  771. ouid = nuid = NO_QUOTA_CHANGE;
  772. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  773. ogid = ngid = NO_QUOTA_CHANGE;
  774. gfs2_alloc_get(ip);
  775. error = gfs2_quota_lock(ip, nuid, ngid);
  776. if (error)
  777. goto out_alloc;
  778. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  779. error = gfs2_quota_check(ip, nuid, ngid);
  780. if (error)
  781. goto out_gunlock_q;
  782. }
  783. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  784. if (error)
  785. goto out_gunlock_q;
  786. error = gfs2_meta_inode_buffer(ip, &dibh);
  787. if (error)
  788. goto out_end_trans;
  789. error = inode_setattr(inode, attr);
  790. gfs2_assert_warn(sdp, !error);
  791. gfs2_inode_attr_out(ip);
  792. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  793. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  794. brelse(dibh);
  795. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  796. gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
  797. gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
  798. }
  799. out_end_trans:
  800. gfs2_trans_end(sdp);
  801. out_gunlock_q:
  802. gfs2_quota_unlock(ip);
  803. out_alloc:
  804. gfs2_alloc_put(ip);
  805. return error;
  806. }
  807. /**
  808. * gfs2_setattr - Change attributes on an inode
  809. * @dentry: The dentry which is changing
  810. * @attr: The structure describing the change
  811. *
  812. * The VFS layer wants to change one or more of an inodes attributes. Write
  813. * that change out to disk.
  814. *
  815. * Returns: errno
  816. */
  817. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  818. {
  819. struct inode *inode = dentry->d_inode;
  820. struct gfs2_inode *ip = GFS2_I(inode);
  821. struct gfs2_holder i_gh;
  822. int error;
  823. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  824. if (error)
  825. return error;
  826. error = -EPERM;
  827. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  828. goto out;
  829. error = inode_change_ok(inode, attr);
  830. if (error)
  831. goto out;
  832. if (attr->ia_valid & ATTR_SIZE)
  833. error = setattr_size(inode, attr);
  834. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  835. error = setattr_chown(inode, attr);
  836. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  837. error = gfs2_acl_chmod(ip, attr);
  838. else
  839. error = gfs2_setattr_simple(ip, attr);
  840. out:
  841. gfs2_glock_dq_uninit(&i_gh);
  842. if (!error)
  843. mark_inode_dirty(inode);
  844. return error;
  845. }
  846. /**
  847. * gfs2_getattr - Read out an inode's attributes
  848. * @mnt: ?
  849. * @dentry: The dentry to stat
  850. * @stat: The inode's stats
  851. *
  852. * Returns: errno
  853. */
  854. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  855. struct kstat *stat)
  856. {
  857. struct inode *inode = dentry->d_inode;
  858. struct gfs2_inode *ip = GFS2_I(inode);
  859. struct gfs2_holder gh;
  860. int error;
  861. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  862. if (!error) {
  863. generic_fillattr(inode, stat);
  864. gfs2_glock_dq_uninit(&gh);
  865. }
  866. return error;
  867. }
  868. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  869. const void *data, size_t size, int flags)
  870. {
  871. struct inode *inode = dentry->d_inode;
  872. struct gfs2_ea_request er;
  873. memset(&er, 0, sizeof(struct gfs2_ea_request));
  874. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  875. if (er.er_type == GFS2_EATYPE_UNUSED)
  876. return -EOPNOTSUPP;
  877. er.er_data = (char *)data;
  878. er.er_name_len = strlen(er.er_name);
  879. er.er_data_len = size;
  880. er.er_flags = flags;
  881. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  882. return gfs2_ea_set(GFS2_I(inode), &er);
  883. }
  884. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  885. void *data, size_t size)
  886. {
  887. struct gfs2_ea_request er;
  888. memset(&er, 0, sizeof(struct gfs2_ea_request));
  889. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  890. if (er.er_type == GFS2_EATYPE_UNUSED)
  891. return -EOPNOTSUPP;
  892. er.er_data = data;
  893. er.er_name_len = strlen(er.er_name);
  894. er.er_data_len = size;
  895. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  896. }
  897. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  898. {
  899. struct gfs2_ea_request er;
  900. memset(&er, 0, sizeof(struct gfs2_ea_request));
  901. er.er_data = (size) ? buffer : NULL;
  902. er.er_data_len = size;
  903. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  904. }
  905. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  906. {
  907. struct gfs2_ea_request er;
  908. memset(&er, 0, sizeof(struct gfs2_ea_request));
  909. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  910. if (er.er_type == GFS2_EATYPE_UNUSED)
  911. return -EOPNOTSUPP;
  912. er.er_name_len = strlen(er.er_name);
  913. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  914. }
  915. struct inode_operations gfs2_file_iops = {
  916. .permission = gfs2_permission,
  917. .setattr = gfs2_setattr,
  918. .getattr = gfs2_getattr,
  919. .setxattr = gfs2_setxattr,
  920. .getxattr = gfs2_getxattr,
  921. .listxattr = gfs2_listxattr,
  922. .removexattr = gfs2_removexattr,
  923. };
  924. struct inode_operations gfs2_dev_iops = {
  925. .permission = gfs2_permission,
  926. .setattr = gfs2_setattr,
  927. .getattr = gfs2_getattr,
  928. .setxattr = gfs2_setxattr,
  929. .getxattr = gfs2_getxattr,
  930. .listxattr = gfs2_listxattr,
  931. .removexattr = gfs2_removexattr,
  932. };
  933. struct inode_operations gfs2_dir_iops = {
  934. .create = gfs2_create,
  935. .lookup = gfs2_lookup,
  936. .link = gfs2_link,
  937. .unlink = gfs2_unlink,
  938. .symlink = gfs2_symlink,
  939. .mkdir = gfs2_mkdir,
  940. .rmdir = gfs2_rmdir,
  941. .mknod = gfs2_mknod,
  942. .rename = gfs2_rename,
  943. .permission = gfs2_permission,
  944. .setattr = gfs2_setattr,
  945. .getattr = gfs2_getattr,
  946. .setxattr = gfs2_setxattr,
  947. .getxattr = gfs2_getxattr,
  948. .listxattr = gfs2_listxattr,
  949. .removexattr = gfs2_removexattr,
  950. };
  951. struct inode_operations gfs2_symlink_iops = {
  952. .readlink = gfs2_readlink,
  953. .follow_link = gfs2_follow_link,
  954. .permission = gfs2_permission,
  955. .setattr = gfs2_setattr,
  956. .getattr = gfs2_getattr,
  957. .setxattr = gfs2_setxattr,
  958. .getxattr = gfs2_getxattr,
  959. .listxattr = gfs2_listxattr,
  960. .removexattr = gfs2_removexattr,
  961. };