inode.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2007
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/pagemap.h>
  24. #include <asm/div64.h>
  25. #include "cifsfs.h"
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. #include "cifs_fs_sb.h"
  31. static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
  32. {
  33. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  34. switch (inode->i_mode & S_IFMT) {
  35. case S_IFREG:
  36. inode->i_op = &cifs_file_inode_ops;
  37. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  38. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  39. inode->i_fop = &cifs_file_direct_nobrl_ops;
  40. else
  41. inode->i_fop = &cifs_file_direct_ops;
  42. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  43. inode->i_fop = &cifs_file_nobrl_ops;
  44. else { /* not direct, send byte range locks */
  45. inode->i_fop = &cifs_file_ops;
  46. }
  47. /* check if server can support readpages */
  48. if (cifs_sb->tcon->ses->server->maxBuf <
  49. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
  50. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  51. else
  52. inode->i_data.a_ops = &cifs_addr_ops;
  53. break;
  54. case S_IFDIR:
  55. #ifdef CONFIG_CIFS_DFS_UPCALL
  56. if (is_dfs_referral) {
  57. inode->i_op = &cifs_dfs_referral_inode_operations;
  58. } else {
  59. #else /* NO DFS support, treat as a directory */
  60. {
  61. #endif
  62. inode->i_op = &cifs_dir_inode_ops;
  63. inode->i_fop = &cifs_dir_ops;
  64. }
  65. break;
  66. case S_IFLNK:
  67. inode->i_op = &cifs_symlink_inode_ops;
  68. break;
  69. default:
  70. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  71. break;
  72. }
  73. }
  74. static void cifs_unix_info_to_inode(struct inode *inode,
  75. FILE_UNIX_BASIC_INFO *info, int force_uid_gid)
  76. {
  77. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  78. struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
  79. __u64 num_of_bytes = le64_to_cpu(info->NumOfBytes);
  80. __u64 end_of_file = le64_to_cpu(info->EndOfFile);
  81. inode->i_atime = cifs_NTtimeToUnix(le64_to_cpu(info->LastAccessTime));
  82. inode->i_mtime =
  83. cifs_NTtimeToUnix(le64_to_cpu(info->LastModificationTime));
  84. inode->i_ctime = cifs_NTtimeToUnix(le64_to_cpu(info->LastStatusChange));
  85. inode->i_mode = le64_to_cpu(info->Permissions);
  86. /*
  87. * Since we set the inode type below we need to mask off
  88. * to avoid strange results if bits set above.
  89. */
  90. inode->i_mode &= ~S_IFMT;
  91. switch (le32_to_cpu(info->Type)) {
  92. case UNIX_FILE:
  93. inode->i_mode |= S_IFREG;
  94. break;
  95. case UNIX_SYMLINK:
  96. inode->i_mode |= S_IFLNK;
  97. break;
  98. case UNIX_DIR:
  99. inode->i_mode |= S_IFDIR;
  100. break;
  101. case UNIX_CHARDEV:
  102. inode->i_mode |= S_IFCHR;
  103. inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  104. le64_to_cpu(info->DevMinor) & MINORMASK);
  105. break;
  106. case UNIX_BLOCKDEV:
  107. inode->i_mode |= S_IFBLK;
  108. inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  109. le64_to_cpu(info->DevMinor) & MINORMASK);
  110. break;
  111. case UNIX_FIFO:
  112. inode->i_mode |= S_IFIFO;
  113. break;
  114. case UNIX_SOCKET:
  115. inode->i_mode |= S_IFSOCK;
  116. break;
  117. default:
  118. /* safest to call it a file if we do not know */
  119. inode->i_mode |= S_IFREG;
  120. cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
  121. break;
  122. }
  123. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) &&
  124. !force_uid_gid)
  125. inode->i_uid = cifs_sb->mnt_uid;
  126. else
  127. inode->i_uid = le64_to_cpu(info->Uid);
  128. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) &&
  129. !force_uid_gid)
  130. inode->i_gid = cifs_sb->mnt_gid;
  131. else
  132. inode->i_gid = le64_to_cpu(info->Gid);
  133. inode->i_nlink = le64_to_cpu(info->Nlinks);
  134. spin_lock(&inode->i_lock);
  135. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  136. /*
  137. * We can not safely change the file size here if the client
  138. * is writing to it due to potential races.
  139. */
  140. i_size_write(inode, end_of_file);
  141. /*
  142. * i_blocks is not related to (i_size / i_blksize),
  143. * but instead 512 byte (2**9) size is required for
  144. * calculating num blocks.
  145. */
  146. inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  147. }
  148. spin_unlock(&inode->i_lock);
  149. }
  150. /*
  151. * Needed to setup inode data for the directory which is the
  152. * junction to the new submount (ie to setup the fake directory
  153. * which represents a DFS referral)
  154. */
  155. static void fill_fake_finddataunix(FILE_UNIX_BASIC_INFO *pfnd_dat,
  156. struct super_block *sb)
  157. {
  158. struct inode *pinode = NULL;
  159. memset(pfnd_dat, 0, sizeof(FILE_UNIX_BASIC_INFO));
  160. /* __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
  161. __le64 pfnd_dat->NumOfBytes = cpu_to_le64(0);
  162. __u64 UniqueId = 0; */
  163. pfnd_dat->LastStatusChange =
  164. cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  165. pfnd_dat->LastAccessTime =
  166. cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  167. pfnd_dat->LastModificationTime =
  168. cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  169. pfnd_dat->Type = cpu_to_le32(UNIX_DIR);
  170. pfnd_dat->Permissions = cpu_to_le64(S_IXUGO | S_IRWXU);
  171. pfnd_dat->Nlinks = cpu_to_le64(2);
  172. if (sb->s_root)
  173. pinode = sb->s_root->d_inode;
  174. if (pinode == NULL)
  175. return;
  176. /* fill in default values for the remaining based on root
  177. inode since we can not query the server for this inode info */
  178. pfnd_dat->DevMajor = cpu_to_le64(MAJOR(pinode->i_rdev));
  179. pfnd_dat->DevMinor = cpu_to_le64(MINOR(pinode->i_rdev));
  180. pfnd_dat->Uid = cpu_to_le64(pinode->i_uid);
  181. pfnd_dat->Gid = cpu_to_le64(pinode->i_gid);
  182. }
  183. int cifs_get_inode_info_unix(struct inode **pinode,
  184. const unsigned char *full_path, struct super_block *sb, int xid)
  185. {
  186. int rc = 0;
  187. FILE_UNIX_BASIC_INFO find_data;
  188. struct cifsTconInfo *pTcon;
  189. struct inode *inode;
  190. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  191. bool is_dfs_referral = false;
  192. struct cifsInodeInfo *cifsInfo;
  193. __u64 num_of_bytes;
  194. __u64 end_of_file;
  195. pTcon = cifs_sb->tcon;
  196. cFYI(1, ("Getting info on %s", full_path));
  197. /* could have done a find first instead but this returns more info */
  198. rc = CIFSSMBUnixQPathInfo(xid, pTcon, full_path, &find_data,
  199. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  200. CIFS_MOUNT_MAP_SPECIAL_CHR);
  201. if (rc == -EREMOTE && !is_dfs_referral) {
  202. is_dfs_referral = true;
  203. cFYI(DBG2, ("DFS ref"));
  204. /* for DFS, server does not give us real inode data */
  205. fill_fake_finddataunix(&find_data, sb);
  206. rc = 0;
  207. } else if (rc)
  208. goto cgiiu_exit;
  209. num_of_bytes = le64_to_cpu(find_data.NumOfBytes);
  210. end_of_file = le64_to_cpu(find_data.EndOfFile);
  211. /* get new inode */
  212. if (*pinode == NULL) {
  213. *pinode = new_inode(sb);
  214. if (*pinode == NULL) {
  215. rc = -ENOMEM;
  216. goto cgiiu_exit;
  217. }
  218. /* Is an i_ino of zero legal? */
  219. /* note ino incremented to unique num in new_inode */
  220. /* Are there sanity checks we can use to ensure that
  221. the server is really filling in that field? */
  222. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  223. (*pinode)->i_ino = (unsigned long)find_data.UniqueId;
  224. if (sb->s_flags & MS_NOATIME)
  225. (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
  226. insert_inode_hash(*pinode);
  227. }
  228. inode = *pinode;
  229. cifsInfo = CIFS_I(inode);
  230. cFYI(1, ("Old time %ld", cifsInfo->time));
  231. cifsInfo->time = jiffies;
  232. cFYI(1, ("New time %ld", cifsInfo->time));
  233. /* this is ok to set on every inode revalidate */
  234. atomic_set(&cifsInfo->inUse, 1);
  235. cifs_unix_info_to_inode(inode, &find_data, 0);
  236. if (num_of_bytes < end_of_file)
  237. cFYI(1, ("allocation size less than end of file"));
  238. cFYI(1, ("Size %ld and blocks %llu",
  239. (unsigned long) inode->i_size,
  240. (unsigned long long)inode->i_blocks));
  241. cifs_set_ops(inode, is_dfs_referral);
  242. cgiiu_exit:
  243. return rc;
  244. }
  245. static int decode_sfu_inode(struct inode *inode, __u64 size,
  246. const unsigned char *path,
  247. struct cifs_sb_info *cifs_sb, int xid)
  248. {
  249. int rc;
  250. int oplock = 0;
  251. __u16 netfid;
  252. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  253. char buf[24];
  254. unsigned int bytes_read;
  255. char *pbuf;
  256. pbuf = buf;
  257. if (size == 0) {
  258. inode->i_mode |= S_IFIFO;
  259. return 0;
  260. } else if (size < 8) {
  261. return -EINVAL; /* EOPNOTSUPP? */
  262. }
  263. rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
  264. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  265. cifs_sb->local_nls,
  266. cifs_sb->mnt_cifs_flags &
  267. CIFS_MOUNT_MAP_SPECIAL_CHR);
  268. if (rc == 0) {
  269. int buf_type = CIFS_NO_BUFFER;
  270. /* Read header */
  271. rc = CIFSSMBRead(xid, pTcon,
  272. netfid,
  273. 24 /* length */, 0 /* offset */,
  274. &bytes_read, &pbuf, &buf_type);
  275. if ((rc == 0) && (bytes_read >= 8)) {
  276. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  277. cFYI(1, ("Block device"));
  278. inode->i_mode |= S_IFBLK;
  279. if (bytes_read == 24) {
  280. /* we have enough to decode dev num */
  281. __u64 mjr; /* major */
  282. __u64 mnr; /* minor */
  283. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  284. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  285. inode->i_rdev = MKDEV(mjr, mnr);
  286. }
  287. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  288. cFYI(1, ("Char device"));
  289. inode->i_mode |= S_IFCHR;
  290. if (bytes_read == 24) {
  291. /* we have enough to decode dev num */
  292. __u64 mjr; /* major */
  293. __u64 mnr; /* minor */
  294. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  295. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  296. inode->i_rdev = MKDEV(mjr, mnr);
  297. }
  298. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  299. cFYI(1, ("Symlink"));
  300. inode->i_mode |= S_IFLNK;
  301. } else {
  302. inode->i_mode |= S_IFREG; /* file? */
  303. rc = -EOPNOTSUPP;
  304. }
  305. } else {
  306. inode->i_mode |= S_IFREG; /* then it is a file */
  307. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  308. }
  309. CIFSSMBClose(xid, pTcon, netfid);
  310. }
  311. return rc;
  312. }
  313. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  314. static int get_sfu_mode(struct inode *inode,
  315. const unsigned char *path,
  316. struct cifs_sb_info *cifs_sb, int xid)
  317. {
  318. #ifdef CONFIG_CIFS_XATTR
  319. ssize_t rc;
  320. char ea_value[4];
  321. __u32 mode;
  322. rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
  323. ea_value, 4 /* size of buf */, cifs_sb->local_nls,
  324. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  325. if (rc < 0)
  326. return (int)rc;
  327. else if (rc > 3) {
  328. mode = le32_to_cpu(*((__le32 *)ea_value));
  329. inode->i_mode &= ~SFBITS_MASK;
  330. cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
  331. inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
  332. cFYI(1, ("special mode bits 0%o", mode));
  333. return 0;
  334. } else {
  335. return 0;
  336. }
  337. #else
  338. return -EOPNOTSUPP;
  339. #endif
  340. }
  341. /*
  342. * Needed to setup inode data for the directory which is the
  343. * junction to the new submount (ie to setup the fake directory
  344. * which represents a DFS referral)
  345. */
  346. static void fill_fake_finddata(FILE_ALL_INFO *pfnd_dat,
  347. struct super_block *sb)
  348. {
  349. memset(pfnd_dat, 0, sizeof(FILE_ALL_INFO));
  350. /* __le64 pfnd_dat->AllocationSize = cpu_to_le64(0);
  351. __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
  352. __u8 pfnd_dat->DeletePending = 0;
  353. __u8 pfnd_data->Directory = 0;
  354. __le32 pfnd_dat->EASize = 0;
  355. __u64 pfnd_dat->IndexNumber = 0;
  356. __u64 pfnd_dat->IndexNumber1 = 0; */
  357. pfnd_dat->CreationTime =
  358. cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  359. pfnd_dat->LastAccessTime =
  360. cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  361. pfnd_dat->LastWriteTime =
  362. cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  363. pfnd_dat->ChangeTime =
  364. cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  365. pfnd_dat->Attributes = cpu_to_le32(ATTR_DIRECTORY);
  366. pfnd_dat->NumberOfLinks = cpu_to_le32(2);
  367. }
  368. int cifs_get_inode_info(struct inode **pinode,
  369. const unsigned char *full_path, FILE_ALL_INFO *pfindData,
  370. struct super_block *sb, int xid, const __u16 *pfid)
  371. {
  372. int rc = 0;
  373. __u32 attr;
  374. struct cifsInodeInfo *cifsInfo;
  375. struct cifsTconInfo *pTcon;
  376. struct inode *inode;
  377. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  378. char *buf = NULL;
  379. bool adjustTZ = false;
  380. bool is_dfs_referral = false;
  381. umode_t default_mode;
  382. pTcon = cifs_sb->tcon;
  383. cFYI(1, ("Getting info on %s", full_path));
  384. if ((pfindData == NULL) && (*pinode != NULL)) {
  385. if (CIFS_I(*pinode)->clientCanCacheRead) {
  386. cFYI(1, ("No need to revalidate cached inode sizes"));
  387. return rc;
  388. }
  389. }
  390. /* if file info not passed in then get it from server */
  391. if (pfindData == NULL) {
  392. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  393. if (buf == NULL)
  394. return -ENOMEM;
  395. pfindData = (FILE_ALL_INFO *)buf;
  396. /* could do find first instead but this returns more info */
  397. rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
  398. 0 /* not legacy */,
  399. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  400. CIFS_MOUNT_MAP_SPECIAL_CHR);
  401. /* BB optimize code so we do not make the above call
  402. when server claims no NT SMB support and the above call
  403. failed at least once - set flag in tcon or mount */
  404. if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
  405. rc = SMBQueryInformation(xid, pTcon, full_path,
  406. pfindData, cifs_sb->local_nls,
  407. cifs_sb->mnt_cifs_flags &
  408. CIFS_MOUNT_MAP_SPECIAL_CHR);
  409. adjustTZ = true;
  410. }
  411. }
  412. /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
  413. if (rc == -EREMOTE) {
  414. is_dfs_referral = true;
  415. fill_fake_finddata(pfindData, sb);
  416. rc = 0;
  417. } else if (rc)
  418. goto cgii_exit;
  419. attr = le32_to_cpu(pfindData->Attributes);
  420. /* get new inode */
  421. if (*pinode == NULL) {
  422. *pinode = new_inode(sb);
  423. if (*pinode == NULL) {
  424. rc = -ENOMEM;
  425. goto cgii_exit;
  426. }
  427. /* Is an i_ino of zero legal? Can we use that to check
  428. if the server supports returning inode numbers? Are
  429. there other sanity checks we can use to ensure that
  430. the server is really filling in that field? */
  431. /* We can not use the IndexNumber field by default from
  432. Windows or Samba (in ALL_INFO buf) but we can request
  433. it explicitly. It may not be unique presumably if
  434. the server has multiple devices mounted under one share */
  435. /* There may be higher info levels that work but are
  436. there Windows server or network appliances for which
  437. IndexNumber field is not guaranteed unique? */
  438. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  439. int rc1 = 0;
  440. __u64 inode_num;
  441. rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
  442. full_path, &inode_num,
  443. cifs_sb->local_nls,
  444. cifs_sb->mnt_cifs_flags &
  445. CIFS_MOUNT_MAP_SPECIAL_CHR);
  446. if (rc1) {
  447. cFYI(1, ("GetSrvInodeNum rc %d", rc1));
  448. /* BB EOPNOSUPP disable SERVER_INUM? */
  449. } else /* do we need cast or hash to ino? */
  450. (*pinode)->i_ino = inode_num;
  451. } /* else ino incremented to unique num in new_inode*/
  452. if (sb->s_flags & MS_NOATIME)
  453. (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
  454. insert_inode_hash(*pinode);
  455. }
  456. inode = *pinode;
  457. cifsInfo = CIFS_I(inode);
  458. cifsInfo->cifsAttrs = attr;
  459. cFYI(1, ("Old time %ld", cifsInfo->time));
  460. cifsInfo->time = jiffies;
  461. cFYI(1, ("New time %ld", cifsInfo->time));
  462. /* blksize needs to be multiple of two. So safer to default to
  463. blksize and blkbits set in superblock so 2**blkbits and blksize
  464. will match rather than setting to:
  465. (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
  466. /* Linux can not store file creation time so ignore it */
  467. if (pfindData->LastAccessTime)
  468. inode->i_atime = cifs_NTtimeToUnix
  469. (le64_to_cpu(pfindData->LastAccessTime));
  470. else /* do not need to use current_fs_time - time not stored */
  471. inode->i_atime = CURRENT_TIME;
  472. inode->i_mtime =
  473. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
  474. inode->i_ctime =
  475. cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
  476. cFYI(DBG2, ("Attributes came in as 0x%x", attr));
  477. if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
  478. inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
  479. inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
  480. }
  481. /* get default inode mode */
  482. if (attr & ATTR_DIRECTORY)
  483. default_mode = cifs_sb->mnt_dir_mode;
  484. else
  485. default_mode = cifs_sb->mnt_file_mode;
  486. /* set permission bits */
  487. if (atomic_read(&cifsInfo->inUse) == 0 ||
  488. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  489. inode->i_mode = default_mode;
  490. else {
  491. /* just reenable write bits if !ATTR_READONLY */
  492. if ((inode->i_mode & S_IWUGO) == 0 &&
  493. (attr & ATTR_READONLY) == 0)
  494. inode->i_mode |= (S_IWUGO & default_mode);
  495. inode->i_mode &= ~S_IFMT;
  496. }
  497. /* clear write bits if ATTR_READONLY is set */
  498. if (attr & ATTR_READONLY)
  499. inode->i_mode &= ~S_IWUGO;
  500. /* set inode type */
  501. if ((attr & ATTR_SYSTEM) &&
  502. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
  503. /* no need to fix endianness on 0 */
  504. if (pfindData->EndOfFile == 0)
  505. inode->i_mode |= S_IFIFO;
  506. else if (decode_sfu_inode(inode,
  507. le64_to_cpu(pfindData->EndOfFile),
  508. full_path, cifs_sb, xid))
  509. cFYI(1, ("unknown SFU file type\n"));
  510. } else {
  511. if (attr & ATTR_DIRECTORY)
  512. inode->i_mode |= S_IFDIR;
  513. else
  514. inode->i_mode |= S_IFREG;
  515. }
  516. spin_lock(&inode->i_lock);
  517. if (is_size_safe_to_change(cifsInfo,
  518. le64_to_cpu(pfindData->EndOfFile))) {
  519. /* can not safely shrink the file size here if the
  520. client is writing to it due to potential races */
  521. i_size_write(inode, le64_to_cpu(pfindData->EndOfFile));
  522. /* 512 bytes (2**9) is the fake blocksize that must be
  523. used for this calculation */
  524. inode->i_blocks = (512 - 1 + le64_to_cpu(
  525. pfindData->AllocationSize)) >> 9;
  526. }
  527. spin_unlock(&inode->i_lock);
  528. inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
  529. /* BB fill in uid and gid here? with help from winbind?
  530. or retrieve from NTFS stream extended attribute */
  531. #ifdef CONFIG_CIFS_EXPERIMENTAL
  532. /* fill in 0777 bits from ACL */
  533. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  534. cFYI(1, ("Getting mode bits from ACL"));
  535. acl_to_uid_mode(inode, full_path, pfid);
  536. }
  537. #endif
  538. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  539. /* fill in remaining high mode bits e.g. SUID, VTX */
  540. get_sfu_mode(inode, full_path, cifs_sb, xid);
  541. } else if (atomic_read(&cifsInfo->inUse) == 0) {
  542. inode->i_uid = cifs_sb->mnt_uid;
  543. inode->i_gid = cifs_sb->mnt_gid;
  544. /* set so we do not keep refreshing these fields with
  545. bad data after user has changed them in memory */
  546. atomic_set(&cifsInfo->inUse, 1);
  547. }
  548. cifs_set_ops(inode, is_dfs_referral);
  549. cgii_exit:
  550. kfree(buf);
  551. return rc;
  552. }
  553. static const struct inode_operations cifs_ipc_inode_ops = {
  554. .lookup = cifs_lookup,
  555. };
  556. /* gets root inode */
  557. struct inode *cifs_iget(struct super_block *sb, unsigned long ino)
  558. {
  559. int xid;
  560. struct cifs_sb_info *cifs_sb;
  561. struct inode *inode;
  562. long rc;
  563. inode = iget_locked(sb, ino);
  564. if (!inode)
  565. return ERR_PTR(-ENOMEM);
  566. if (!(inode->i_state & I_NEW))
  567. return inode;
  568. cifs_sb = CIFS_SB(inode->i_sb);
  569. xid = GetXid();
  570. if (cifs_sb->tcon->unix_ext)
  571. rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid);
  572. else
  573. rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid,
  574. NULL);
  575. if (rc && cifs_sb->tcon->ipc) {
  576. cFYI(1, ("ipc connection - fake read inode"));
  577. inode->i_mode |= S_IFDIR;
  578. inode->i_nlink = 2;
  579. inode->i_op = &cifs_ipc_inode_ops;
  580. inode->i_fop = &simple_dir_operations;
  581. inode->i_uid = cifs_sb->mnt_uid;
  582. inode->i_gid = cifs_sb->mnt_gid;
  583. } else if (rc) {
  584. _FreeXid(xid);
  585. iget_failed(inode);
  586. return ERR_PTR(rc);
  587. }
  588. unlock_new_inode(inode);
  589. /* can not call macro FreeXid here since in a void func
  590. * TODO: This is no longer true
  591. */
  592. _FreeXid(xid);
  593. return inode;
  594. }
  595. int cifs_unlink(struct inode *inode, struct dentry *direntry)
  596. {
  597. int rc = 0;
  598. int xid;
  599. struct cifs_sb_info *cifs_sb;
  600. struct cifsTconInfo *pTcon;
  601. char *full_path = NULL;
  602. struct cifsInodeInfo *cifsInode;
  603. FILE_BASIC_INFO *pinfo_buf;
  604. cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
  605. xid = GetXid();
  606. if (inode)
  607. cifs_sb = CIFS_SB(inode->i_sb);
  608. else
  609. cifs_sb = CIFS_SB(direntry->d_sb);
  610. pTcon = cifs_sb->tcon;
  611. /* Unlink can be called from rename so we can not grab the sem here
  612. since we deadlock otherwise */
  613. /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
  614. full_path = build_path_from_dentry(direntry);
  615. /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
  616. if (full_path == NULL) {
  617. FreeXid(xid);
  618. return -ENOMEM;
  619. }
  620. if ((pTcon->ses->capabilities & CAP_UNIX) &&
  621. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  622. le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
  623. rc = CIFSPOSIXDelFile(xid, pTcon, full_path,
  624. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  625. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  626. cFYI(1, ("posix del rc %d", rc));
  627. if ((rc == 0) || (rc == -ENOENT))
  628. goto psx_del_no_retry;
  629. }
  630. rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
  631. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  632. psx_del_no_retry:
  633. if (!rc) {
  634. if (direntry->d_inode)
  635. drop_nlink(direntry->d_inode);
  636. } else if (rc == -ENOENT) {
  637. d_drop(direntry);
  638. } else if (rc == -ETXTBSY) {
  639. int oplock = 0;
  640. __u16 netfid;
  641. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
  642. CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
  643. &netfid, &oplock, NULL, cifs_sb->local_nls,
  644. cifs_sb->mnt_cifs_flags &
  645. CIFS_MOUNT_MAP_SPECIAL_CHR);
  646. if (rc == 0) {
  647. CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
  648. cifs_sb->local_nls,
  649. cifs_sb->mnt_cifs_flags &
  650. CIFS_MOUNT_MAP_SPECIAL_CHR);
  651. CIFSSMBClose(xid, pTcon, netfid);
  652. if (direntry->d_inode)
  653. drop_nlink(direntry->d_inode);
  654. }
  655. } else if (rc == -EACCES) {
  656. /* try only if r/o attribute set in local lookup data? */
  657. pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
  658. if (pinfo_buf) {
  659. /* ATTRS set to normal clears r/o bit */
  660. pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
  661. if (!(pTcon->ses->flags & CIFS_SES_NT4))
  662. rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
  663. pinfo_buf,
  664. cifs_sb->local_nls,
  665. cifs_sb->mnt_cifs_flags &
  666. CIFS_MOUNT_MAP_SPECIAL_CHR);
  667. else
  668. rc = -EOPNOTSUPP;
  669. if (rc == -EOPNOTSUPP) {
  670. int oplock = 0;
  671. __u16 netfid;
  672. /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
  673. full_path,
  674. (__u16)ATTR_NORMAL,
  675. cifs_sb->local_nls);
  676. For some strange reason it seems that NT4 eats the
  677. old setattr call without actually setting the
  678. attributes so on to the third attempted workaround
  679. */
  680. /* BB could scan to see if we already have it open
  681. and pass in pid of opener to function */
  682. rc = CIFSSMBOpen(xid, pTcon, full_path,
  683. FILE_OPEN, SYNCHRONIZE |
  684. FILE_WRITE_ATTRIBUTES, 0,
  685. &netfid, &oplock, NULL,
  686. cifs_sb->local_nls,
  687. cifs_sb->mnt_cifs_flags &
  688. CIFS_MOUNT_MAP_SPECIAL_CHR);
  689. if (rc == 0) {
  690. rc = CIFSSMBSetFileInfo(xid, pTcon,
  691. pinfo_buf,
  692. netfid,
  693. current->tgid);
  694. CIFSSMBClose(xid, pTcon, netfid);
  695. }
  696. }
  697. kfree(pinfo_buf);
  698. }
  699. if (rc == 0) {
  700. rc = CIFSSMBDelFile(xid, pTcon, full_path,
  701. cifs_sb->local_nls,
  702. cifs_sb->mnt_cifs_flags &
  703. CIFS_MOUNT_MAP_SPECIAL_CHR);
  704. if (!rc) {
  705. if (direntry->d_inode)
  706. drop_nlink(direntry->d_inode);
  707. } else if (rc == -ETXTBSY) {
  708. int oplock = 0;
  709. __u16 netfid;
  710. rc = CIFSSMBOpen(xid, pTcon, full_path,
  711. FILE_OPEN, DELETE,
  712. CREATE_NOT_DIR |
  713. CREATE_DELETE_ON_CLOSE,
  714. &netfid, &oplock, NULL,
  715. cifs_sb->local_nls,
  716. cifs_sb->mnt_cifs_flags &
  717. CIFS_MOUNT_MAP_SPECIAL_CHR);
  718. if (rc == 0) {
  719. CIFSSMBRenameOpenFile(xid, pTcon,
  720. netfid, NULL,
  721. cifs_sb->local_nls,
  722. cifs_sb->mnt_cifs_flags &
  723. CIFS_MOUNT_MAP_SPECIAL_CHR);
  724. CIFSSMBClose(xid, pTcon, netfid);
  725. if (direntry->d_inode)
  726. drop_nlink(direntry->d_inode);
  727. }
  728. /* BB if rc = -ETXTBUSY goto the rename logic BB */
  729. }
  730. }
  731. }
  732. if (direntry->d_inode) {
  733. cifsInode = CIFS_I(direntry->d_inode);
  734. cifsInode->time = 0; /* will force revalidate to get info
  735. when needed */
  736. direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
  737. }
  738. if (inode) {
  739. inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
  740. cifsInode = CIFS_I(inode);
  741. cifsInode->time = 0; /* force revalidate of dir as well */
  742. }
  743. kfree(full_path);
  744. FreeXid(xid);
  745. return rc;
  746. }
  747. static void posix_fill_in_inode(struct inode *tmp_inode,
  748. FILE_UNIX_BASIC_INFO *pData, int isNewInode)
  749. {
  750. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  751. loff_t local_size;
  752. struct timespec local_mtime;
  753. cifsInfo->time = jiffies;
  754. atomic_inc(&cifsInfo->inUse);
  755. /* save mtime and size */
  756. local_mtime = tmp_inode->i_mtime;
  757. local_size = tmp_inode->i_size;
  758. cifs_unix_info_to_inode(tmp_inode, pData, 1);
  759. cifs_set_ops(tmp_inode, false);
  760. if (!S_ISREG(tmp_inode->i_mode))
  761. return;
  762. /*
  763. * No sense invalidating pages for new inode
  764. * since we we have not started caching
  765. * readahead file data yet.
  766. */
  767. if (isNewInode)
  768. return;
  769. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  770. (local_size == tmp_inode->i_size)) {
  771. cFYI(1, ("inode exists but unchanged"));
  772. } else {
  773. /* file may have changed on server */
  774. cFYI(1, ("invalidate inode, readdir detected change"));
  775. invalidate_remote_inode(tmp_inode);
  776. }
  777. }
  778. int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
  779. {
  780. int rc = 0;
  781. int xid;
  782. struct cifs_sb_info *cifs_sb;
  783. struct cifsTconInfo *pTcon;
  784. char *full_path = NULL;
  785. struct inode *newinode = NULL;
  786. cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
  787. xid = GetXid();
  788. cifs_sb = CIFS_SB(inode->i_sb);
  789. pTcon = cifs_sb->tcon;
  790. full_path = build_path_from_dentry(direntry);
  791. if (full_path == NULL) {
  792. FreeXid(xid);
  793. return -ENOMEM;
  794. }
  795. if ((pTcon->ses->capabilities & CAP_UNIX) &&
  796. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  797. le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
  798. u32 oplock = 0;
  799. FILE_UNIX_BASIC_INFO *pInfo =
  800. kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  801. if (pInfo == NULL) {
  802. rc = -ENOMEM;
  803. goto mkdir_out;
  804. }
  805. mode &= ~current->fs->umask;
  806. rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
  807. mode, NULL /* netfid */, pInfo, &oplock,
  808. full_path, cifs_sb->local_nls,
  809. cifs_sb->mnt_cifs_flags &
  810. CIFS_MOUNT_MAP_SPECIAL_CHR);
  811. if (rc == -EOPNOTSUPP) {
  812. kfree(pInfo);
  813. goto mkdir_retry_old;
  814. } else if (rc) {
  815. cFYI(1, ("posix mkdir returned 0x%x", rc));
  816. d_drop(direntry);
  817. } else {
  818. if (pInfo->Type == cpu_to_le32(-1)) {
  819. /* no return info, go query for it */
  820. kfree(pInfo);
  821. goto mkdir_get_info;
  822. }
  823. /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
  824. to set uid/gid */
  825. inc_nlink(inode);
  826. if (pTcon->nocase)
  827. direntry->d_op = &cifs_ci_dentry_ops;
  828. else
  829. direntry->d_op = &cifs_dentry_ops;
  830. newinode = new_inode(inode->i_sb);
  831. if (newinode == NULL) {
  832. kfree(pInfo);
  833. goto mkdir_get_info;
  834. }
  835. /* Is an i_ino of zero legal? */
  836. /* Are there sanity checks we can use to ensure that
  837. the server is really filling in that field? */
  838. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  839. newinode->i_ino =
  840. (unsigned long)pInfo->UniqueId;
  841. } /* note ino incremented to unique num in new_inode */
  842. if (inode->i_sb->s_flags & MS_NOATIME)
  843. newinode->i_flags |= S_NOATIME | S_NOCMTIME;
  844. newinode->i_nlink = 2;
  845. insert_inode_hash(newinode);
  846. d_instantiate(direntry, newinode);
  847. /* we already checked in POSIXCreate whether
  848. frame was long enough */
  849. posix_fill_in_inode(direntry->d_inode,
  850. pInfo, 1 /* NewInode */);
  851. #ifdef CONFIG_CIFS_DEBUG2
  852. cFYI(1, ("instantiated dentry %p %s to inode %p",
  853. direntry, direntry->d_name.name, newinode));
  854. if (newinode->i_nlink != 2)
  855. cFYI(1, ("unexpected number of links %d",
  856. newinode->i_nlink));
  857. #endif
  858. }
  859. kfree(pInfo);
  860. goto mkdir_out;
  861. }
  862. mkdir_retry_old:
  863. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  864. rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
  865. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  866. if (rc) {
  867. cFYI(1, ("cifs_mkdir returned 0x%x", rc));
  868. d_drop(direntry);
  869. } else {
  870. mkdir_get_info:
  871. inc_nlink(inode);
  872. if (pTcon->unix_ext)
  873. rc = cifs_get_inode_info_unix(&newinode, full_path,
  874. inode->i_sb, xid);
  875. else
  876. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  877. inode->i_sb, xid, NULL);
  878. if (pTcon->nocase)
  879. direntry->d_op = &cifs_ci_dentry_ops;
  880. else
  881. direntry->d_op = &cifs_dentry_ops;
  882. d_instantiate(direntry, newinode);
  883. /* setting nlink not necessary except in cases where we
  884. * failed to get it from the server or was set bogus */
  885. if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
  886. direntry->d_inode->i_nlink = 2;
  887. mode &= ~current->fs->umask;
  888. /* must turn on setgid bit if parent dir has it */
  889. if (inode->i_mode & S_ISGID)
  890. mode |= S_ISGID;
  891. if (pTcon->unix_ext) {
  892. struct cifs_unix_set_info_args args = {
  893. .mode = mode,
  894. .ctime = NO_CHANGE_64,
  895. .atime = NO_CHANGE_64,
  896. .mtime = NO_CHANGE_64,
  897. .device = 0,
  898. };
  899. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  900. args.uid = (__u64)current->fsuid;
  901. if (inode->i_mode & S_ISGID)
  902. args.gid = (__u64)inode->i_gid;
  903. else
  904. args.gid = (__u64)current->fsgid;
  905. } else {
  906. args.uid = NO_CHANGE_64;
  907. args.gid = NO_CHANGE_64;
  908. }
  909. CIFSSMBUnixSetInfo(xid, pTcon, full_path, &args,
  910. cifs_sb->local_nls,
  911. cifs_sb->mnt_cifs_flags &
  912. CIFS_MOUNT_MAP_SPECIAL_CHR);
  913. } else {
  914. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  915. (mode & S_IWUGO) == 0) {
  916. FILE_BASIC_INFO pInfo;
  917. memset(&pInfo, 0, sizeof(pInfo));
  918. pInfo.Attributes = cpu_to_le32(ATTR_READONLY);
  919. CIFSSMBSetPathInfo(xid, pTcon, full_path,
  920. &pInfo, cifs_sb->local_nls,
  921. cifs_sb->mnt_cifs_flags &
  922. CIFS_MOUNT_MAP_SPECIAL_CHR);
  923. }
  924. if (direntry->d_inode) {
  925. if (cifs_sb->mnt_cifs_flags &
  926. CIFS_MOUNT_DYNPERM)
  927. direntry->d_inode->i_mode =
  928. (mode | S_IFDIR);
  929. if (cifs_sb->mnt_cifs_flags &
  930. CIFS_MOUNT_SET_UID) {
  931. direntry->d_inode->i_uid =
  932. current->fsuid;
  933. if (inode->i_mode & S_ISGID)
  934. direntry->d_inode->i_gid =
  935. inode->i_gid;
  936. else
  937. direntry->d_inode->i_gid =
  938. current->fsgid;
  939. }
  940. }
  941. }
  942. }
  943. mkdir_out:
  944. kfree(full_path);
  945. FreeXid(xid);
  946. return rc;
  947. }
  948. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  949. {
  950. int rc = 0;
  951. int xid;
  952. struct cifs_sb_info *cifs_sb;
  953. struct cifsTconInfo *pTcon;
  954. char *full_path = NULL;
  955. struct cifsInodeInfo *cifsInode;
  956. cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
  957. xid = GetXid();
  958. cifs_sb = CIFS_SB(inode->i_sb);
  959. pTcon = cifs_sb->tcon;
  960. full_path = build_path_from_dentry(direntry);
  961. if (full_path == NULL) {
  962. FreeXid(xid);
  963. return -ENOMEM;
  964. }
  965. rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
  966. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  967. if (!rc) {
  968. drop_nlink(inode);
  969. spin_lock(&direntry->d_inode->i_lock);
  970. i_size_write(direntry->d_inode, 0);
  971. clear_nlink(direntry->d_inode);
  972. spin_unlock(&direntry->d_inode->i_lock);
  973. }
  974. cifsInode = CIFS_I(direntry->d_inode);
  975. cifsInode->time = 0; /* force revalidate to go get info when
  976. needed */
  977. direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
  978. current_fs_time(inode->i_sb);
  979. kfree(full_path);
  980. FreeXid(xid);
  981. return rc;
  982. }
  983. int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
  984. struct inode *target_inode, struct dentry *target_direntry)
  985. {
  986. char *fromName;
  987. char *toName;
  988. struct cifs_sb_info *cifs_sb_source;
  989. struct cifs_sb_info *cifs_sb_target;
  990. struct cifsTconInfo *pTcon;
  991. int xid;
  992. int rc = 0;
  993. xid = GetXid();
  994. cifs_sb_target = CIFS_SB(target_inode->i_sb);
  995. cifs_sb_source = CIFS_SB(source_inode->i_sb);
  996. pTcon = cifs_sb_source->tcon;
  997. if (pTcon != cifs_sb_target->tcon) {
  998. FreeXid(xid);
  999. return -EXDEV; /* BB actually could be allowed if same server,
  1000. but different share.
  1001. Might eventually add support for this */
  1002. }
  1003. /* we already have the rename sem so we do not need to grab it again
  1004. here to protect the path integrity */
  1005. fromName = build_path_from_dentry(source_direntry);
  1006. toName = build_path_from_dentry(target_direntry);
  1007. if ((fromName == NULL) || (toName == NULL)) {
  1008. rc = -ENOMEM;
  1009. goto cifs_rename_exit;
  1010. }
  1011. rc = CIFSSMBRename(xid, pTcon, fromName, toName,
  1012. cifs_sb_source->local_nls,
  1013. cifs_sb_source->mnt_cifs_flags &
  1014. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1015. if (rc == -EEXIST) {
  1016. /* check if they are the same file because rename of hardlinked
  1017. files is a noop */
  1018. FILE_UNIX_BASIC_INFO *info_buf_source;
  1019. FILE_UNIX_BASIC_INFO *info_buf_target;
  1020. info_buf_source =
  1021. kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  1022. if (info_buf_source != NULL) {
  1023. info_buf_target = info_buf_source + 1;
  1024. if (pTcon->unix_ext)
  1025. rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
  1026. info_buf_source,
  1027. cifs_sb_source->local_nls,
  1028. cifs_sb_source->mnt_cifs_flags &
  1029. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1030. /* else rc is still EEXIST so will fall through to
  1031. unlink the target and retry rename */
  1032. if (rc == 0) {
  1033. rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
  1034. info_buf_target,
  1035. cifs_sb_target->local_nls,
  1036. /* remap based on source sb */
  1037. cifs_sb_source->mnt_cifs_flags &
  1038. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1039. }
  1040. if ((rc == 0) &&
  1041. (info_buf_source->UniqueId ==
  1042. info_buf_target->UniqueId)) {
  1043. /* do not rename since the files are hardlinked which
  1044. is a noop */
  1045. } else {
  1046. /* we either can not tell the files are hardlinked
  1047. (as with Windows servers) or files are not
  1048. hardlinked so delete the target manually before
  1049. renaming to follow POSIX rather than Windows
  1050. semantics */
  1051. cifs_unlink(target_inode, target_direntry);
  1052. rc = CIFSSMBRename(xid, pTcon, fromName,
  1053. toName,
  1054. cifs_sb_source->local_nls,
  1055. cifs_sb_source->mnt_cifs_flags
  1056. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  1057. }
  1058. kfree(info_buf_source);
  1059. } /* if we can not get memory just leave rc as EEXIST */
  1060. }
  1061. if (rc)
  1062. cFYI(1, ("rename rc %d", rc));
  1063. if ((rc == -EIO) || (rc == -EEXIST)) {
  1064. int oplock = 0;
  1065. __u16 netfid;
  1066. /* BB FIXME Is Generic Read correct for rename? */
  1067. /* if renaming directory - we should not say CREATE_NOT_DIR,
  1068. need to test renaming open directory, also GENERIC_READ
  1069. might not right be right access to request */
  1070. rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
  1071. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  1072. cifs_sb_source->local_nls,
  1073. cifs_sb_source->mnt_cifs_flags &
  1074. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1075. if (rc == 0) {
  1076. rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
  1077. cifs_sb_source->local_nls,
  1078. cifs_sb_source->mnt_cifs_flags &
  1079. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1080. CIFSSMBClose(xid, pTcon, netfid);
  1081. }
  1082. }
  1083. cifs_rename_exit:
  1084. kfree(fromName);
  1085. kfree(toName);
  1086. FreeXid(xid);
  1087. return rc;
  1088. }
  1089. int cifs_revalidate(struct dentry *direntry)
  1090. {
  1091. int xid;
  1092. int rc = 0, wbrc = 0;
  1093. char *full_path;
  1094. struct cifs_sb_info *cifs_sb;
  1095. struct cifsInodeInfo *cifsInode;
  1096. loff_t local_size;
  1097. struct timespec local_mtime;
  1098. bool invalidate_inode = false;
  1099. if (direntry->d_inode == NULL)
  1100. return -ENOENT;
  1101. cifsInode = CIFS_I(direntry->d_inode);
  1102. if (cifsInode == NULL)
  1103. return -ENOENT;
  1104. /* no sense revalidating inode info on file that no one can write */
  1105. if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
  1106. return rc;
  1107. xid = GetXid();
  1108. cifs_sb = CIFS_SB(direntry->d_sb);
  1109. /* can not safely grab the rename sem here if rename calls revalidate
  1110. since that would deadlock */
  1111. full_path = build_path_from_dentry(direntry);
  1112. if (full_path == NULL) {
  1113. FreeXid(xid);
  1114. return -ENOMEM;
  1115. }
  1116. cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
  1117. "jiffies %ld", full_path, direntry->d_inode,
  1118. direntry->d_inode->i_count.counter, direntry,
  1119. direntry->d_time, jiffies));
  1120. if (cifsInode->time == 0) {
  1121. /* was set to zero previously to force revalidate */
  1122. } else if (time_before(jiffies, cifsInode->time + HZ) &&
  1123. lookupCacheEnabled) {
  1124. if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
  1125. (direntry->d_inode->i_nlink == 1)) {
  1126. kfree(full_path);
  1127. FreeXid(xid);
  1128. return rc;
  1129. } else {
  1130. cFYI(1, ("Have to revalidate file due to hardlinks"));
  1131. }
  1132. }
  1133. /* save mtime and size */
  1134. local_mtime = direntry->d_inode->i_mtime;
  1135. local_size = direntry->d_inode->i_size;
  1136. if (cifs_sb->tcon->unix_ext) {
  1137. rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
  1138. direntry->d_sb, xid);
  1139. if (rc) {
  1140. cFYI(1, ("error on getting revalidate info %d", rc));
  1141. /* if (rc != -ENOENT)
  1142. rc = 0; */ /* BB should we cache info on
  1143. certain errors? */
  1144. }
  1145. } else {
  1146. rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
  1147. direntry->d_sb, xid, NULL);
  1148. if (rc) {
  1149. cFYI(1, ("error on getting revalidate info %d", rc));
  1150. /* if (rc != -ENOENT)
  1151. rc = 0; */ /* BB should we cache info on
  1152. certain errors? */
  1153. }
  1154. }
  1155. /* should we remap certain errors, access denied?, to zero */
  1156. /* if not oplocked, we invalidate inode pages if mtime or file size
  1157. had changed on server */
  1158. if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
  1159. (local_size == direntry->d_inode->i_size)) {
  1160. cFYI(1, ("cifs_revalidate - inode unchanged"));
  1161. } else {
  1162. /* file may have changed on server */
  1163. if (cifsInode->clientCanCacheRead) {
  1164. /* no need to invalidate inode pages since we were the
  1165. only ones who could have modified the file and the
  1166. server copy is staler than ours */
  1167. } else {
  1168. invalidate_inode = true;
  1169. }
  1170. }
  1171. /* can not grab this sem since kernel filesys locking documentation
  1172. indicates i_mutex may be taken by the kernel on lookup and rename
  1173. which could deadlock if we grab the i_mutex here as well */
  1174. /* mutex_lock(&direntry->d_inode->i_mutex);*/
  1175. /* need to write out dirty pages here */
  1176. if (direntry->d_inode->i_mapping) {
  1177. /* do we need to lock inode until after invalidate completes
  1178. below? */
  1179. wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
  1180. if (wbrc)
  1181. CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
  1182. }
  1183. if (invalidate_inode) {
  1184. /* shrink_dcache not necessary now that cifs dentry ops
  1185. are exported for negative dentries */
  1186. /* if (S_ISDIR(direntry->d_inode->i_mode))
  1187. shrink_dcache_parent(direntry); */
  1188. if (S_ISREG(direntry->d_inode->i_mode)) {
  1189. if (direntry->d_inode->i_mapping) {
  1190. wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
  1191. if (wbrc)
  1192. CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
  1193. }
  1194. /* may eventually have to do this for open files too */
  1195. if (list_empty(&(cifsInode->openFileList))) {
  1196. /* changed on server - flush read ahead pages */
  1197. cFYI(1, ("Invalidating read ahead data on "
  1198. "closed file"));
  1199. invalidate_remote_inode(direntry->d_inode);
  1200. }
  1201. }
  1202. }
  1203. /* mutex_unlock(&direntry->d_inode->i_mutex); */
  1204. kfree(full_path);
  1205. FreeXid(xid);
  1206. return rc;
  1207. }
  1208. int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1209. struct kstat *stat)
  1210. {
  1211. int err = cifs_revalidate(dentry);
  1212. if (!err) {
  1213. generic_fillattr(dentry->d_inode, stat);
  1214. stat->blksize = CIFS_MAX_MSGSIZE;
  1215. }
  1216. return err;
  1217. }
  1218. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1219. {
  1220. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1221. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  1222. struct page *page;
  1223. int rc = 0;
  1224. page = grab_cache_page(mapping, index);
  1225. if (!page)
  1226. return -ENOMEM;
  1227. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  1228. unlock_page(page);
  1229. page_cache_release(page);
  1230. return rc;
  1231. }
  1232. static int cifs_vmtruncate(struct inode *inode, loff_t offset)
  1233. {
  1234. struct address_space *mapping = inode->i_mapping;
  1235. unsigned long limit;
  1236. spin_lock(&inode->i_lock);
  1237. if (inode->i_size < offset)
  1238. goto do_expand;
  1239. /*
  1240. * truncation of in-use swapfiles is disallowed - it would cause
  1241. * subsequent swapout to scribble on the now-freed blocks.
  1242. */
  1243. if (IS_SWAPFILE(inode)) {
  1244. spin_unlock(&inode->i_lock);
  1245. goto out_busy;
  1246. }
  1247. i_size_write(inode, offset);
  1248. spin_unlock(&inode->i_lock);
  1249. /*
  1250. * unmap_mapping_range is called twice, first simply for efficiency
  1251. * so that truncate_inode_pages does fewer single-page unmaps. However
  1252. * after this first call, and before truncate_inode_pages finishes,
  1253. * it is possible for private pages to be COWed, which remain after
  1254. * truncate_inode_pages finishes, hence the second unmap_mapping_range
  1255. * call must be made for correctness.
  1256. */
  1257. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1258. truncate_inode_pages(mapping, offset);
  1259. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1260. goto out_truncate;
  1261. do_expand:
  1262. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1263. if (limit != RLIM_INFINITY && offset > limit) {
  1264. spin_unlock(&inode->i_lock);
  1265. goto out_sig;
  1266. }
  1267. if (offset > inode->i_sb->s_maxbytes) {
  1268. spin_unlock(&inode->i_lock);
  1269. goto out_big;
  1270. }
  1271. i_size_write(inode, offset);
  1272. spin_unlock(&inode->i_lock);
  1273. out_truncate:
  1274. if (inode->i_op && inode->i_op->truncate)
  1275. inode->i_op->truncate(inode);
  1276. return 0;
  1277. out_sig:
  1278. send_sig(SIGXFSZ, current, 0);
  1279. out_big:
  1280. return -EFBIG;
  1281. out_busy:
  1282. return -ETXTBSY;
  1283. }
  1284. static int
  1285. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  1286. int xid, char *full_path)
  1287. {
  1288. int rc;
  1289. struct cifsFileInfo *open_file;
  1290. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1291. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1292. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1293. /*
  1294. * To avoid spurious oplock breaks from server, in the case of
  1295. * inodes that we already have open, avoid doing path based
  1296. * setting of file size if we can do it by handle.
  1297. * This keeps our caching token (oplock) and avoids timeouts
  1298. * when the local oplock break takes longer to flush
  1299. * writebehind data than the SMB timeout for the SetPathInfo
  1300. * request would allow
  1301. */
  1302. open_file = find_writable_file(cifsInode);
  1303. if (open_file) {
  1304. __u16 nfid = open_file->netfid;
  1305. __u32 npid = open_file->pid;
  1306. rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid,
  1307. npid, false);
  1308. atomic_dec(&open_file->wrtPending);
  1309. cFYI(1, ("SetFSize for attrs rc = %d", rc));
  1310. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1311. unsigned int bytes_written;
  1312. rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size,
  1313. &bytes_written, NULL, NULL, 1);
  1314. cFYI(1, ("Wrt seteof rc %d", rc));
  1315. }
  1316. } else
  1317. rc = -EINVAL;
  1318. if (rc != 0) {
  1319. /* Set file size by pathname rather than by handle
  1320. either because no valid, writeable file handle for
  1321. it was found or because there was an error setting
  1322. it by handle */
  1323. rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size,
  1324. false, cifs_sb->local_nls,
  1325. cifs_sb->mnt_cifs_flags &
  1326. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1327. cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
  1328. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1329. __u16 netfid;
  1330. int oplock = 0;
  1331. rc = SMBLegacyOpen(xid, pTcon, full_path,
  1332. FILE_OPEN, GENERIC_WRITE,
  1333. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  1334. cifs_sb->local_nls,
  1335. cifs_sb->mnt_cifs_flags &
  1336. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1337. if (rc == 0) {
  1338. unsigned int bytes_written;
  1339. rc = CIFSSMBWrite(xid, pTcon, netfid, 0,
  1340. attrs->ia_size,
  1341. &bytes_written, NULL,
  1342. NULL, 1);
  1343. cFYI(1, ("wrt seteof rc %d", rc));
  1344. CIFSSMBClose(xid, pTcon, netfid);
  1345. }
  1346. }
  1347. }
  1348. if (rc == 0) {
  1349. rc = cifs_vmtruncate(inode, attrs->ia_size);
  1350. cifs_truncate_page(inode->i_mapping, inode->i_size);
  1351. }
  1352. return rc;
  1353. }
  1354. static int
  1355. cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
  1356. char *full_path, __u32 dosattr)
  1357. {
  1358. int rc;
  1359. int oplock = 0;
  1360. __u16 netfid;
  1361. __u32 netpid;
  1362. bool set_time = false;
  1363. struct cifsFileInfo *open_file;
  1364. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1365. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1366. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1367. FILE_BASIC_INFO info_buf;
  1368. if (attrs->ia_valid & ATTR_ATIME) {
  1369. set_time = true;
  1370. info_buf.LastAccessTime =
  1371. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  1372. } else
  1373. info_buf.LastAccessTime = 0;
  1374. if (attrs->ia_valid & ATTR_MTIME) {
  1375. set_time = true;
  1376. info_buf.LastWriteTime =
  1377. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  1378. } else
  1379. info_buf.LastWriteTime = 0;
  1380. /*
  1381. * Samba throws this field away, but windows may actually use it.
  1382. * Do not set ctime unless other time stamps are changed explicitly
  1383. * (i.e. by utimes()) since we would then have a mix of client and
  1384. * server times.
  1385. */
  1386. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  1387. cFYI(1, ("CIFS - CTIME changed"));
  1388. info_buf.ChangeTime =
  1389. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  1390. } else
  1391. info_buf.ChangeTime = 0;
  1392. info_buf.CreationTime = 0; /* don't change */
  1393. info_buf.Attributes = cpu_to_le32(dosattr);
  1394. /*
  1395. * If the file is already open for write, just use that fileid
  1396. */
  1397. open_file = find_writable_file(cifsInode);
  1398. if (open_file) {
  1399. netfid = open_file->netfid;
  1400. netpid = open_file->pid;
  1401. goto set_via_filehandle;
  1402. }
  1403. /*
  1404. * NT4 apparently returns success on this call, but it doesn't
  1405. * really work.
  1406. */
  1407. if (!(pTcon->ses->flags & CIFS_SES_NT4)) {
  1408. rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
  1409. &info_buf, cifs_sb->local_nls,
  1410. cifs_sb->mnt_cifs_flags &
  1411. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1412. if (rc != -EOPNOTSUPP && rc != -EINVAL)
  1413. goto out;
  1414. }
  1415. cFYI(1, ("calling SetFileInfo since SetPathInfo for "
  1416. "times not supported by this server"));
  1417. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
  1418. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  1419. CREATE_NOT_DIR, &netfid, &oplock,
  1420. NULL, cifs_sb->local_nls,
  1421. cifs_sb->mnt_cifs_flags &
  1422. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1423. if (rc != 0) {
  1424. if (rc == -EIO)
  1425. rc = -EINVAL;
  1426. goto out;
  1427. }
  1428. netpid = current->tgid;
  1429. set_via_filehandle:
  1430. rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
  1431. if (open_file == NULL)
  1432. CIFSSMBClose(xid, pTcon, netfid);
  1433. else
  1434. atomic_dec(&open_file->wrtPending);
  1435. out:
  1436. return rc;
  1437. }
  1438. static int
  1439. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  1440. {
  1441. int rc;
  1442. int xid;
  1443. char *full_path = NULL;
  1444. struct inode *inode = direntry->d_inode;
  1445. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1446. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1447. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1448. struct cifs_unix_set_info_args *args = NULL;
  1449. cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x",
  1450. direntry->d_name.name, attrs->ia_valid));
  1451. xid = GetXid();
  1452. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1453. /* check if we have permission to change attrs */
  1454. rc = inode_change_ok(inode, attrs);
  1455. if (rc < 0)
  1456. goto out;
  1457. else
  1458. rc = 0;
  1459. }
  1460. full_path = build_path_from_dentry(direntry);
  1461. if (full_path == NULL) {
  1462. rc = -ENOMEM;
  1463. goto out;
  1464. }
  1465. if ((attrs->ia_valid & ATTR_MTIME) || (attrs->ia_valid & ATTR_SIZE)) {
  1466. /*
  1467. Flush data before changing file size or changing the last
  1468. write time of the file on the server. If the
  1469. flush returns error, store it to report later and continue.
  1470. BB: This should be smarter. Why bother flushing pages that
  1471. will be truncated anyway? Also, should we error out here if
  1472. the flush returns error?
  1473. */
  1474. rc = filemap_write_and_wait(inode->i_mapping);
  1475. if (rc != 0) {
  1476. cifsInode->write_behind_rc = rc;
  1477. rc = 0;
  1478. }
  1479. }
  1480. if (attrs->ia_valid & ATTR_SIZE) {
  1481. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1482. if (rc != 0)
  1483. goto out;
  1484. }
  1485. /* skip mode change if it's just for clearing setuid/setgid */
  1486. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1487. attrs->ia_valid &= ~ATTR_MODE;
  1488. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1489. if (args == NULL) {
  1490. rc = -ENOMEM;
  1491. goto out;
  1492. }
  1493. /* set up the struct */
  1494. if (attrs->ia_valid & ATTR_MODE)
  1495. args->mode = attrs->ia_mode;
  1496. else
  1497. args->mode = NO_CHANGE_64;
  1498. if (attrs->ia_valid & ATTR_UID)
  1499. args->uid = attrs->ia_uid;
  1500. else
  1501. args->uid = NO_CHANGE_64;
  1502. if (attrs->ia_valid & ATTR_GID)
  1503. args->gid = attrs->ia_gid;
  1504. else
  1505. args->gid = NO_CHANGE_64;
  1506. if (attrs->ia_valid & ATTR_ATIME)
  1507. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  1508. else
  1509. args->atime = NO_CHANGE_64;
  1510. if (attrs->ia_valid & ATTR_MTIME)
  1511. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  1512. else
  1513. args->mtime = NO_CHANGE_64;
  1514. if (attrs->ia_valid & ATTR_CTIME)
  1515. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  1516. else
  1517. args->ctime = NO_CHANGE_64;
  1518. args->device = 0;
  1519. rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path, args,
  1520. cifs_sb->local_nls,
  1521. cifs_sb->mnt_cifs_flags &
  1522. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1523. if (!rc)
  1524. rc = inode_setattr(inode, attrs);
  1525. out:
  1526. kfree(args);
  1527. kfree(full_path);
  1528. FreeXid(xid);
  1529. return rc;
  1530. }
  1531. static int
  1532. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  1533. {
  1534. int xid;
  1535. struct inode *inode = direntry->d_inode;
  1536. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1537. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1538. char *full_path = NULL;
  1539. int rc = -EACCES;
  1540. __u32 dosattr = 0;
  1541. __u64 mode = NO_CHANGE_64;
  1542. xid = GetXid();
  1543. cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
  1544. direntry->d_name.name, attrs->ia_valid));
  1545. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1546. /* check if we have permission to change attrs */
  1547. rc = inode_change_ok(inode, attrs);
  1548. if (rc < 0) {
  1549. FreeXid(xid);
  1550. return rc;
  1551. } else
  1552. rc = 0;
  1553. }
  1554. full_path = build_path_from_dentry(direntry);
  1555. if (full_path == NULL) {
  1556. FreeXid(xid);
  1557. return -ENOMEM;
  1558. }
  1559. if ((attrs->ia_valid & ATTR_MTIME) || (attrs->ia_valid & ATTR_SIZE)) {
  1560. /*
  1561. Flush data before changing file size or changing the last
  1562. write time of the file on the server. If the
  1563. flush returns error, store it to report later and continue.
  1564. BB: This should be smarter. Why bother flushing pages that
  1565. will be truncated anyway? Also, should we error out here if
  1566. the flush returns error?
  1567. */
  1568. rc = filemap_write_and_wait(inode->i_mapping);
  1569. if (rc != 0) {
  1570. cifsInode->write_behind_rc = rc;
  1571. rc = 0;
  1572. }
  1573. }
  1574. if (attrs->ia_valid & ATTR_SIZE) {
  1575. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1576. if (rc != 0)
  1577. goto cifs_setattr_exit;
  1578. }
  1579. /*
  1580. * Without unix extensions we can't send ownership changes to the
  1581. * server, so silently ignore them. This is consistent with how
  1582. * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With
  1583. * CIFSACL support + proper Windows to Unix idmapping, we may be
  1584. * able to support this in the future.
  1585. */
  1586. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  1587. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  1588. /* skip mode change if it's just for clearing setuid/setgid */
  1589. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1590. attrs->ia_valid &= ~ATTR_MODE;
  1591. if (attrs->ia_valid & ATTR_MODE) {
  1592. cFYI(1, ("Mode changed to 0%o", attrs->ia_mode));
  1593. mode = attrs->ia_mode;
  1594. }
  1595. if (attrs->ia_valid & ATTR_MODE) {
  1596. rc = 0;
  1597. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1598. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  1599. rc = mode_to_acl(inode, full_path, mode);
  1600. else
  1601. #endif
  1602. if (((mode & S_IWUGO) == 0) &&
  1603. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  1604. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  1605. /* fix up mode if we're not using dynperm */
  1606. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  1607. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  1608. } else if ((mode & S_IWUGO) &&
  1609. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  1610. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  1611. /* Attributes of 0 are ignored */
  1612. if (dosattr == 0)
  1613. dosattr |= ATTR_NORMAL;
  1614. /* reset local inode permissions to normal */
  1615. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1616. attrs->ia_mode &= ~(S_IALLUGO);
  1617. if (S_ISDIR(inode->i_mode))
  1618. attrs->ia_mode |=
  1619. cifs_sb->mnt_dir_mode;
  1620. else
  1621. attrs->ia_mode |=
  1622. cifs_sb->mnt_file_mode;
  1623. }
  1624. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1625. /* ignore mode change - ATTR_READONLY hasn't changed */
  1626. attrs->ia_valid &= ~ATTR_MODE;
  1627. }
  1628. }
  1629. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  1630. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  1631. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1632. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  1633. /* Even if error on time set, no sense failing the call if
  1634. the server would set the time to a reasonable value anyway,
  1635. and this check ensures that we are not being called from
  1636. sys_utimes in which case we ought to fail the call back to
  1637. the user when the server rejects the call */
  1638. if ((rc) && (attrs->ia_valid &
  1639. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  1640. rc = 0;
  1641. }
  1642. /* do not need local check to inode_check_ok since the server does
  1643. that */
  1644. if (!rc)
  1645. rc = inode_setattr(inode, attrs);
  1646. cifs_setattr_exit:
  1647. kfree(full_path);
  1648. FreeXid(xid);
  1649. return rc;
  1650. }
  1651. int
  1652. cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  1653. {
  1654. struct inode *inode = direntry->d_inode;
  1655. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1656. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1657. if (pTcon->unix_ext)
  1658. return cifs_setattr_unix(direntry, attrs);
  1659. return cifs_setattr_nounix(direntry, attrs);
  1660. /* BB: add cifs_setattr_legacy for really old servers */
  1661. }
  1662. #if 0
  1663. void cifs_delete_inode(struct inode *inode)
  1664. {
  1665. cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
  1666. /* may have to add back in if and when safe distributed caching of
  1667. directories added e.g. via FindNotify */
  1668. }
  1669. #endif