ops_inode.c 26 KB

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